data.table

Assign a matrix to a subset of a data.table

≡放荡痞女 提交于 2020-01-14 08:05:30
问题 I would like to assign a matrix to a multi-column subset of a data.table but the matrix ends up getting treated as a column vector. For example, dt1 <- data.table(a1=rnorm(5), a2=rnorm(5), a3=rnorm(5)) m1 <- matrix(rnorm(10), ncol=2) dt1[,c("a1","a2")] <- m1 Warning messages: 1: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, : 2 column matrix RHS of := will be treated as one vector 2: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :

Assign a matrix to a subset of a data.table

依然范特西╮ 提交于 2020-01-14 08:04:21
问题 I would like to assign a matrix to a multi-column subset of a data.table but the matrix ends up getting treated as a column vector. For example, dt1 <- data.table(a1=rnorm(5), a2=rnorm(5), a3=rnorm(5)) m1 <- matrix(rnorm(10), ncol=2) dt1[,c("a1","a2")] <- m1 Warning messages: 1: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, : 2 column matrix RHS of := will be treated as one vector 2: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :

Assign a matrix to a subset of a data.table

元气小坏坏 提交于 2020-01-14 08:04:13
问题 I would like to assign a matrix to a multi-column subset of a data.table but the matrix ends up getting treated as a column vector. For example, dt1 <- data.table(a1=rnorm(5), a2=rnorm(5), a3=rnorm(5)) m1 <- matrix(rnorm(10), ncol=2) dt1[,c("a1","a2")] <- m1 Warning messages: 1: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, : 2 column matrix RHS of := will be treated as one vector 2: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :

Reformat data tables based on row names to generate new columns in R

偶尔善良 提交于 2020-01-14 03:09:21
问题 I have a data frame that looks like m1 m2 m3 P001.st 60.00 2.0 1 P003.nd 14.30 2.077 1 P003.rt 29.60 2.077 1 P006.st 10.30 2.077 1 P006.nd 79.30 2.077 1 P008.nd 9.16 2.077 1 I want to reformat table so that only first part (before period, i.e., P001, P003 etc) of the row name appear as row names and append the each subsequent rows with similar names to columns. The output should look like m1st m2st m3st m1nd m2nd m3nd m1rt m2rt m3rt P001 60.00 2.0 1 0 0 0 0 0 0 P003 0 0 0 14.30 2.077 1 29.60

Reformat data tables based on row names to generate new columns in R

雨燕双飞 提交于 2020-01-14 03:09:08
问题 I have a data frame that looks like m1 m2 m3 P001.st 60.00 2.0 1 P003.nd 14.30 2.077 1 P003.rt 29.60 2.077 1 P006.st 10.30 2.077 1 P006.nd 79.30 2.077 1 P008.nd 9.16 2.077 1 I want to reformat table so that only first part (before period, i.e., P001, P003 etc) of the row name appear as row names and append the each subsequent rows with similar names to columns. The output should look like m1st m2st m3st m1nd m2nd m3nd m1rt m2rt m3rt P001 60.00 2.0 1 0 0 0 0 0 0 P003 0 0 0 14.30 2.077 1 29.60

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

Faster equivalent to group_by %>% expand in R

[亡魂溺海] 提交于 2020-01-13 13:13:36
问题 I am trying to create a sequence of years for multiple IDs in R. My input table has a single row for each ID, and gives a Start_year. It looks like this: ID Start_year 01 1999 02 2004 03 2015 04 2007 etc... I need to create a table with multiple rows for each ID, showing each year from their Start_year up to 2015. I will then use this to join to another table. So in my example, ID1 would have 17 rows with the years 1999:2015. ID2 would have 12 rows 2004:2015, ID3 would have 1 row 2015, and

Faster equivalent to group_by %>% expand in R

混江龙づ霸主 提交于 2020-01-13 13:12:20
问题 I am trying to create a sequence of years for multiple IDs in R. My input table has a single row for each ID, and gives a Start_year. It looks like this: ID Start_year 01 1999 02 2004 03 2015 04 2007 etc... I need to create a table with multiple rows for each ID, showing each year from their Start_year up to 2015. I will then use this to join to another table. So in my example, ID1 would have 17 rows with the years 1999:2015. ID2 would have 12 rows 2004:2015, ID3 would have 1 row 2015, and

How to transfer negative value at current row to previous row in a data frame?

被刻印的时光 ゝ 提交于 2020-01-13 10:36:10
问题 I want to transfer the negative values at the current row to the previous row by adding them to the previous row within each group. Following is the sample raw data I have: raw_data <- data.frame(GROUP = rep(c('A','B','C'),each = 6), YEARMO = rep(c(201801:201806),3), VALUE = c(100,-10,20,70,-50,30,20,60,40,-20,-10,50,0,10,-30,50,100,-100)) > raw_data GROUP YEARMO VALUE 1 A 201801 100 2 A 201802 -10 3 A 201803 20 4 A 201804 70 5 A 201805 -50 6 A 201806 30 7 B 201801 20 8 B 201802 60 9 B 201803

How to identify overlaps in multiple columns

五迷三道 提交于 2020-01-13 08:53:15
问题 I have a dataset ( mydata ) that contains multiple columns which could fit inside ranges that are stored in another dataset ( mycomparison ). I would like to join mycomparison to mydata where the mydata values are within the ranges in mycomparison . MWE library(data.table) mydata<-data.table( id=1:5, val1=seq(10000, 50000, by=10000), val2=floor(rnorm(5,mean=400,sd=100)), val3=rnorm(5,mean=.7,sd=.1) ) mycomparison<-data.table( Name=LETTERS[1:3], minval1=c(0,30000,10000), maxval1=c(50000,80000