如何在 R 中更改多列的数据类型?

How to change multiple columns' data type in R?

我想将所有以 _FL 结尾的字段从字符转换为数字。我认为这段代码可以工作,但事实并非如此:所有这些字段都填满了 NA。怎么了?

library(data.table)
#s = fread('filename.csv',header = TRUE,sep = ";",dec = ".")
s=data.table(ID=(1:10), B=rnorm(10), C_FL=c("I","N"), D_FL=(0:1), E_FL=c("N","I"))
cn=colnames(s)
# Change all fields ending with _FL from "N"/"I" to numeric 0/1
for (i in cn){
  if(substr(i,nchar(i)-2,nchar(i))=='_FL'){
    s[,i] = as.numeric(gsub("I",1,gsub("N",0,s[,i])))
  }
}

一种方法,

library(data.table)
#create function to change the values
f1 <- function(x){ifelse(x == 'N', 1, ifelse(x == 'I', 0, x))}
#get the columns to apply the function
ind <- names(s)[grepl('_fl', names(s))] 

s[, (ind) := lapply(.SD, f1), .SDcols = ind]
#to convert to numeric then,
s[, (ind) := lapply(.SD, as.numeric), .SDcols = ind]

s
#    id           b c_fl d_fl e_fl
# 1:  1  0.20818371    0    0    1
# 2:  2 -0.06470128    1    1    0
# 3:  3 -1.03487884    0    0    1
# 4:  4  1.38119541    1    1    0
# 5:  5 -0.67924124    0    0    1
# 6:  6  0.84424732    1    1    0
# 7:  7 -0.65531266    0    0    1
# 8:  8  0.44867938    1    1    0
# 9:  9  0.15805731    0    0    1
#10: 10 -0.42541642    1    1    0


str(s)
Classes ‘data.table’ and 'data.frame':  10 obs. of  5 variables:
 $ id  : int  1 2 3 4 5 6 7 8 9 10
 $ b   : num  0.7464 -0.7491 -0.7144 0.561 0.0243 ...
 $ c_fl: num  0 1 0 1 0 1 0 1 0 1
 $ d_fl: num  0 1 0 1 0 1 0 1 0 1
 $ e_fl: num  1 0 1 0 1 0 1 0 1 0
 - attr(*, ".internal.selfref")=<externalptr>  

另一种选择是使用intersect()找到包含“_FL”的character列,并根据条件== "N":[=14=将它们转换为二进制列]

library(data.table)

# Find relevant columns
chr.cols <- names(s)[intersect(which(sapply(s,is.character)), 
                           grep("_FL", names(s)))]
# Convert to numeric
for(col in chr.cols) set(s, j = col, value = as.numeric(s[[col]] == "N"))

# See result
> s
    ID          B C_FL D_FL E_FL
 1:  1  0.6175364    0    0    1
 2:  2 -0.9500318    1    1    0
 3:  3 -0.6341547    0    0    1
 4:  4 -0.8055696    1    1    0
 5:  5 -0.3139938    0    0    1
 6:  6  0.4676558    1    1    0
 7:  7  1.6455591    0    0    1
 8:  8 -0.4544377    1    1    0
 9:  9  0.3512442    0    0    1
10: 10  0.3828367    1    1    0