dcast

Conditional merge, based an event happening between two panel observations

懵懂的女人 提交于 2020-01-24 23:58:52
问题 I have a panel dataset: panel and a dataset with a list of events: Events . For the panel dataset, an equal panelID shows that two observations belong together. panelID = c(1:50) year= c(2001:2010) country = c("NLD", "GRC", "GBR") n <- 2 library(data.table) set.seed(123) Panel <- data.table(panelID = rep(sample(panelID), each = n), country = rep(sample(country, length(panelID), replace = T), each = n), year = c(replicate(length(panelID), sample(year, n))), some_NA = sample(0:5, 6), some_NA

Conditional merge, based an event happening between two panel observations

点点圈 提交于 2020-01-24 23:58:06
问题 I have a panel dataset: panel and a dataset with a list of events: Events . For the panel dataset, an equal panelID shows that two observations belong together. panelID = c(1:50) year= c(2001:2010) country = c("NLD", "GRC", "GBR") n <- 2 library(data.table) set.seed(123) Panel <- data.table(panelID = rep(sample(panelID), each = n), country = rep(sample(country, length(panelID), replace = T), each = n), year = c(replicate(length(panelID), sample(year, n))), some_NA = sample(0:5, 6), some_NA

wide format with dcast data.table [closed]

回眸只為那壹抹淺笑 提交于 2020-01-13 18:55:10
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I would like to transform a table like this (*): set.seed(1) mydata <- data.frame(ID=rep(1:4, each=3), R=rep(1:3, times=4), FIXED=rep(runif(4), each=3), AAA=rnorm(12), BBB=rbinom(12,12,0.5), CCC=runif(12)) ID R FIXED AAA BBB CCC 1 1 0.26 -0.83 8 0.82 1 2 0.26 1.59 5 0.64 1 3 0.26 0.32 6 0.78 2 1 0.37 -0.82 6 0

R: Melt and Dcast

╄→гoц情女王★ 提交于 2020-01-11 11:28:30
问题 I have a dataset like this: CASE_ID = c("C1","C1", "C2","C2", "C2", "C3", "C4") PERSON_ID = c(1,0,7,8,1,20,7) PERSON_DIVISION = c("Zone 1", "NA", "Zone 1", "Zone 3", "Zone 1", "Zone 5", "Zone 1") df <- data.frame(CASE_ID, PERSON_ID, PERSON_DIVISION) df That results in: CASE_ID PERSON_ID PERSON_DIVISION 1 C1 1 Zone 1 2 C1 0 NA 3 C2 7 Zone 1 4 C2 8 Zone 3 5 C2 1 Zone 1 6 C3 20 Zone 5 7 C4 7 Zone 1 And I want to transform it in: CASE_ID P1_ID P2_ID P3_ID P1_Division P2_Division P3_Division 1 1 0

Reshape data into long columns based on variable in R for IRR

五迷三道 提交于 2019-12-25 08:06:31
问题 There are thousands of answers describing how to reshape from wide to long and organize by certain variables. I do not know what I'm not wrapping my head around. I need to organize rows that originally begin as rater, obs, val1, val2, etc. Into columns under rater for IRR. Given a format similar to my data that can be created with: r1 <- c('bob', 'sally', "george", "bob", "sally", "george") r2 <- c(1,1,1,2,2,2) r3 <- c("bad", "good", "good", "good", "good", "bad") r4 <- c("bad", "bad", "good"

Error with large numerics in dcast.data.table

浪子不回头ぞ 提交于 2019-12-25 07:30:59
问题 Given a data frame I am trying to cast from long-to-wide using the dcast.data.table function from library(data.table) . However, when using large numeric's on the left side of the formula it some how combines. Below is an example: df <- structure(list(A = c(10000000007624, 10000000007619, 10000000007745, 10000000007624, 10000000007767, 10000000007729, 10000000007705, 10000000007711, 10000000007784, 10000000007745, 10000000007624, 10000000007762, 10000000007762, 10000000007631, 10000000007762,

Elegant solution for casting (spreading) multiple columns of character vectors

限于喜欢 提交于 2019-12-24 09:29:11
问题 I want to transforms a data frame with contact information with of a for a list of municipalities in which similar information such as e.g. phone number appears in multiple columns. I have tried using both reshape2::dcast() as well as tidyr::spread() , neither of which solves my problem. I have also checked other post of stack overflow e.g. Multiple column spread Have yet to find a solution which works. It seems to me that the problems should be fairly straightforward (and solvable with

Reshaping EPA wind speed & direction data with dcast in R

戏子无情 提交于 2019-12-13 16:22:16
问题 I am trying to convert long format wind data into wide format. Both wind speed and wind direction are listed within the Parameter.Name column. These values need to be cast by both Local.Site.Name, and Date.Local variables. If there are multiple observations per unique Local.Site.Name + Date.Local row, then I want the mean value of those observations. The built-in argument 'fun.aggregate = mean' works just fine for wind speed, but mean wind direction cannot be computed this way because the

Data table dcast column headings

落爺英雄遲暮 提交于 2019-12-13 03:07:56
问题 I have a data table of the form ID REGION INCOME_BAND RESIDENCY_YEARS 1 SW Under 5,000 10-15 2 Wales Over 70,000 1-5 3 Center 15,000-19,999 6-9 4 SE 15,000-19,999 15-19 5 North 15,000-19,999 10-15 6 North 15,000-19,999 6-9 created by exp = data.table( ID = c(1,2,3,4,5,6), REGION=c("SW", "Wales", "Center", "SE", "North", "North"), INCOME_BAND = c("Under ?5,000", "Over ?70,000", "?15,000-?19,999", "?15,000-?19,999", "?15,000-?19,999","?15,000-?19,999"), RESIDENCY_YEARS = c("10-15","1-5","6-9",

Dcast/merge based on a column, with a value within a certain range

陌路散爱 提交于 2019-12-11 16:26:07
问题 I have a panel dataset: panel and a dataset with a list of events: Events. For the panel dataset, an equal panelID shows that two observations belong together. panelID = c(1:50) year= c(2001:2010) country = c("NLD", "GRC", "GBR") n <- 2 library(data.table) set.seed(123) Panel <- data.table(panelID = rep(sample(panelID), each = n), country = rep(sample(country, length(panelID), replace = T), each = n), year = c(replicate(length(panelID), sample(year, n))), some_NA = sample(0:5, 6), some_NA