evgenii ershenko evgenii ershenko - 1 year ago 79
R Question

update column value of data.table in R based on another column values


train <- fread(input = "../train.csv", header = TRUE, sep = ",",
stringsAsFactors = FALSE, data.table = TRUE)

train$Response <- "" #create new column with values ""
train$Response <- paste(train[, 15:20, with = FALSE], collapse = "")

RStudio console:
> is.data.frame(train)
[1] TRUE
> is.data.table(train)
[1] TRUE

Columns with indexes from 15 to 20 has values = 1 or 0 of int type.

I have tryied to make one column "response" (character type) with values "0101..." as result of concatenation values 1 and 0 of columns, described above.

After this procedure I see abnormal behaviour of R studio, can't access to train etc.

Maybe something wrong?

Answer Source

We can specify the columns to paste in .SDcols, use paste0 with do.call on the Subset of Data.table (.SD), and assign (:=) to create the 'Response' column.

train[, Response := do.call(paste0, .SD), .SDcols = 15:20]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download