将列分割成R中的多个列 [英] Splitting a column into multiple columns in R
问题描述
我试图将我的数据框中的单个字符变量分割为多个因子变量。
I'm trying to split a single "character" variable in my dataframe into mutiple "factor" variables.
> sampledf=data.frame(vin=c('v1','v2','v3'),features=c('f1:f2:f3','f2:f4:f5','f1:f4:f5'))
> sampledf
vin features
1 v1 f1:f2:f3
2 v2 f2:f4:f5
3 v3 f1:f4:f5
> desireddf=data.frame(vin=c('v1','v2','v3'),f1=c(1,0,1),f2=c(1,1,0),f3=c(1,0,0),f4=c(0,1,1),f5=c(0,1,1))
> desireddf
vin f1 f2 f3 f4 f5
1 v1 1 1 1 0 0
2 v2 0 1 0 1 1
3 v3 1 0 0 1 1
我试过使用strsplit()分离features列(strsplit(as.character(df $ features) ;))但是没有运气因素分解他们
I've tried using strsplit() to separate the "features" column(strsplit(as.character(df$features), ";")) but have had no luck factorizing them
推荐答案
我们可以使用 mtabulate $分割后(
strsplit(..
)'features'列的 qdapTools
中的c $ c>
We can use mtabulate
from qdapTools
after splitting (strsplit(..
) the 'features' column.
library(qdapTools)
cbind(sampledf[1],mtabulate(strsplit(as.character(sampledf$features), ':')))
# vin f1 f2 f3 f4 f5
#1 v1 1 1 1 0 0
#2 v2 0 1 0 1 1
#3 v3 1 0 0 1 1
或者我们可以使用 cSplit_e
code> library(splitstackshape)
Or we can use cSplit_e
from library(splitstackshape)
library(splitstackshape)
df1 <- cSplit_e(sampledf, 'features', ':', type= 'character', fill=0, drop=TRUE)
names(df1) <- sub('.*_', '', names(df1))
或使用 base R
方法,我们 split
如前所述,设置列表的名称
元素从 strsplit
与'vin'列,转换为一个键/值列'data.frame'使用 stack
,得到第一列'sampledf'的表
,转置和 cbind
。
Or using base R
methods, we split
as before, set the names of the list
elements from the strsplit
with 'vin' column, convert to a key/value columns 'data.frame' using stack
, get the table
, transpose and cbind
with the first column of 'sampledf'.
cbind(sampledf[1],
t(table(stack(setNames(strsplit(as.character(sampledf$features), ':'),
sampledf$vin)))))
这篇关于将列分割成R中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!