我想将以_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])))
}
}
答案 0 :(得分:3)
另一种选择是找到包含" _FL"的character
列。使用intersect()
,并根据条件== "N"
将这些列转换为二进制列:
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
答案 1 :(得分:1)
一种方法,
setslot