1

I have a data.table with 83 columns called growthcheck. The vector flag4issues contains a subset of 21 column names of growthcheck. flag4issues is a character vector.

test is another vector having dimension 3 by 21. I'm debugging a code where the line

growthcheck[1:3, flag4issues] <- test 

throws an error.

I tried with tranforming test to data.table, do a loop, but nothing works. Any idea?

dput(growthcheck[1:5,flag4issues]), test and flag4issues can be retrieved here

0

1 Answer 1

1

The data.table syntax would be to specify the row index in i (1:3), assign the data.frame ('test') to the columns specified. As the columns of 'growthcheck' are numeric class and the 'test' are character (matrix), need to convert the class before doing the assignment

library(data.table)
growthcheck <- growthcheck[, lapply(.SD, as.character)]
growthcheck[1:3, (flag4issues) := as.data.frame(test)]

-output

growthcheck[1:3, 1:2]
   Country
1: Austria
2: Austria
3: Belgium
                                                                                                               Obs
1:  3.A.4.3 - Population (POP, Deer): Irregularities in the time series have been been identified. Years flagged: 
2:  3.A.4.3 - Population (POP, Deer): Irregularities in the time series have been been identified. Years flagged: 
3: 3.A.4.4 - Population (POP, Goats): Irregularities in the time series have been been identified. Years flagged: 
Sign up to request clarification or add additional context in comments.

17 Comments

Many thanks ...i have still too many things to learn on data.table
I have been too optimistic...I keep getting the error: Error in `[.data.table`(growthcheck, 1:3, `:=`((flag4issues), as.data.frame(test))) : Supplied 21 columns to be assigned 3 items. Please see NEWS for v1.12.2. any further suggestion?
@efz Is it based on the same dput as I couldn't reproduce that error. The test when converted to data.frame is 3 x 21 and the flag4issues is a vector of 21 elements
well, growcheck has many more columns, for the rest is the same
thanks for your perseverance, your first solution did actually work. I missed the Reduce part of the code that tranformed test from list to matrix. thanks again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.