【发布时间】:2020-05-06 14:21:21
【问题描述】:
我试图用“。”分割数据框中的一些列。然后根据原始名称重命名拆分的列。 Original dataset Expect result
library(ISLR)
wage <- Wage #sample dataset from ISLR
wage_term_ref <- as.data.frame(wage[,3:9]) # These are the columns I need to split
colnames(wage_term_ref)
“maritl”“种族”“教育”“地区”“工作类别”“健康”“health_ins”
wage_term_ref[] <- lapply(wage_term_ref, as.character) # change all from factor to character
martil<- data.frame(do.call(rbind, strsplit(wage_term_ref$maritl, "[.]" ))) # split the first columne
names(martil)<-c("martil_Index","martil_Status") # rename the splited columns based on the original name "martil"
然后我需要对工资期限参考中的余额 6 列重复相同的操作。 最后将所有 _Index 列(例如.martil_Index)和工资[,1:2] 合并到一个新的数据框“wage_updated”
有没有人有更好的方法来做到这一点?也许是一个循环?提前致谢。
【问题讨论】:
标签: r