[英]How to assign multiple columns with a function in data.table list?
我有一個data.table
列表,我想將某些列轉換為數值並執行rowSums
。 我試圖弄清楚如何將所有列轉換為數值。
這就是我嘗試過的。
# Obtain data
tbl<-get_data(sqlquery = tqry, dbase=db1, server=serv)
# Names of the columns that need to be converted to numeric
score<-names(tbl)[grep('score',names(tbl),ignore.case = T)]
tbl[,class(AcceptingNewPatientsScore)]
[1] "character"
### Wrong - Having problem here
tbl[,eval(score):=as.numeric(get(score))]
tbl[,class(AcceptingNewPatientsScore)]
[1] "numeric" # It converted but jumbled scores.
tbl[,tscore:=rowSums(.SD,na.rm = FALSE),.SDcols=score]
感謝@Frank的建議。
tbl[,(score):=lapply(.SD, as.numeric),.SDcols=score]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.