简体   繁体   中英

R Data.table error when creating a new column

on the below code, Data.table is throwing an error when i try to create new columns. May i know how to get through this error. I believe its the problem with the syntax as I'm still a beginner in R. I have read online sources where adding a by argument would resolve but im not sure how to add it.

NF = MFDETAILED[,c(1,2)]
  
  feature_names = colnames(compare)
  NF = cbind(NF, data.table( NF = apply(compare,1,function(x {paste0(feature_names[(which(x[]==1))],collapse = ", ")}), NMF = apply(compare,1,function(x) {paste0(feature_names[(which(x[]==0))],collapse = ", ")})))
#ERROR LINE
  **NF[, c("LOOKUP", "MAT") := tstrsplit(COMBINATION, "-", fixed=TRUE)]**
  NF[, ("COMBINATION"):=NULL]
  setcolorder(NF, c( "LOOKUP","MAT","MS", "NF","NMF"))
  setnames(NF,c("LOOKUP MATERIAL", "MATERIAL", "MATCH SCORE","MATCH FEATURES","NON MATCH FEATURES"))

Error message:- Error in [.data.table (NF, , := (c("LOOKUP", "MAT"), tstrsplit(COMBINATION, : Supplied 2 columns to be assigned 4 items. Please see NEWS for v1.12.2.

First up, get an example dataset:

library(data.table)
dt_main <- data.table(fruits=c("Pears Bananas Grapefruit", "Apples Oranges Kiwi"))

You are using tstrsplit() too split a variable into multiple values. Look at what happens, without assignment - I get three variables:

> dt_main[, tstrsplit(fruits, " ")]
#        V1      V2         V3
# 1:  Pears Bananas Grapefruit
# 2: Apples Oranges       Kiwi

It created two variables. You are doing this, and then trying to assign those values to too few variables:

dt_main[, c("fruit_1", "fruit_2") := tstrsplit(fruits, " ")]
# Error in `[.data.table`(dt_main, , `:=`(c("fruit_1", "fruit_2"), tstrsplit(fruits,  : 
#   Supplied 2 columns to be assigned 3 items. Please see NEWS for v1.12.2.

You need as many variables to be initiated as you are trying to assign:

# dt_main[, c("fruit_1", "fruit_2", "fruit_3") := tstrsplit(fruits, " ")][]
                             fruits   fruit_1   fruit_2      fruit_3
# 1:       Pears Bananas Grapefruit     Pears   Bananas   Grapefruit
# 2:            Apples Oranges Kiwi    Apples   Oranges         Kiwi

PS. The problem is very clearly explained in the error message, you really should have put in a bit of effort to a) find the solution yourself and b) make a reproducible example. Please do so in the future.

Thank you. Adding a by argument to the tstrsplit function resolved the issue. NF[, c("LOOKUP", "MAT"):= tstrsplit(COMBINATION, "-", fixed=TRUE),by =.(COMBINATION)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM