R - 带有二进制和后向量的字符串列表的列

时间:2018-02-23 22:31:15

标签: r dataframe

我有这个代码

dataframe = data.frame(nam= c("stefy, dany", "dany, ari", "ale"), num = c(1,2,3))
dim(dataframe)

library(qdapTools)

dataframe$nam = gsub(" ", "", dataframe$nam, fixed = TRUE)
names1 = mtabulate(  strsplit(dataframe$nam, ','))
names1
myNames = names(names1) # 29 total Genre
myNames


dataframe$nam = apply( names1 , 1 , paste , collapse = "," )

dataframe

数据看起来像这样

         nam num
1 stefy,dany   1
2   dany,ari   2
3        ale   3

  ale ari dany stefy
1   0   0    1     1
2   0   1    1     0
3   1   0    0     0

> myNames = names(names1)
> myNames
[1] "ale"   "ari"   "dany"  "stefy"

      nam num
1 0,0,1,1   1
2 0,1,1,0   2
3 1,0,0,0   3

有一种快速的方法可以将二进制矢量/指标变量与可能值的字典相关联。 此外,如果我想创建一个情节,我想看到字符串不是二进制矢量 谢谢

0 个答案:

没有答案