Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中将多值单元格解析为交叉表_R_Crosstab_Multivalue - Fatal编程技术网

在R中将多值单元格解析为交叉表

在R中将多值单元格解析为交叉表,r,crosstab,multivalue,R,Crosstab,Multivalue,我有一个数据集,当作为CSV导入时,它会为调查问题生成多值单元格,允许多个答案。在R中交叉标记单值答案很简单: ctab(formula = col1 ~ col2, data = tmp) # given dataframe tmp 但我需要如下交叉表数据: tmp2 <- data.frame ( "level" = c ( "dir" , "mgr" , "dir", "vp" ) , "roles" = c ("dev, qa"

我有一个数据集,当作为CSV导入时,它会为调查问题生成多值单元格,允许多个答案。在R中交叉标记单值答案很简单:

ctab(formula = col1 ~ col2, data = tmp) # given dataframe tmp
但我需要如下交叉表数据:

tmp2 <- data.frame ( 
  "level" = c ( "dir" ,    "mgr" ,         "dir",          "vp" ) ,
  "roles" = c  ("dev, qa", "dev, qa, ops", "dev, qa, ops", "dev")
 )
但我需要的是(我不想预先指定dev、qa、ops等):

我尽了最大努力去寻找类似的东西。如果已经回答了,请道歉。谢谢


-ctb

这里有一个使用
tidyr

library(tidyr)
tmp2开发运营质量保证
#>目录2 1 2
#>经理1
#>副总裁100

以下是使用
tidyr

library(tidyr)
tmp2开发运营质量保证
#>目录2 1 2
#>经理1
#>副总裁100

您可以通过先设置索引,然后拆分字符串来实现目标 该索引非常有用,因为它可以防止回收

library(data.table)
setDT(tmp2)
res <- tmp2[,index:=1:.N][,.(value=unlist(strsplit(roles,","))),.(index,level)]

##> res
##    index level value
## 1:     1   dir   dev
## 2:     1   dir    qa
## 3:     2   mgr   dev
## 4:     2   mgr    qa
## 5:     2   mgr   ops
## 6:     3   dir   dev
## 7:     3   dir    qa
## 8:     3   dir   ops
## 9:     4    vp   dev

您可以通过先设置索引,然后拆分字符串来实现目标 该索引非常有用,因为它可以防止回收

library(data.table)
setDT(tmp2)
res <- tmp2[,index:=1:.N][,.(value=unlist(strsplit(roles,","))),.(index,level)]

##> res
##    index level value
## 1:     1   dir   dev
## 2:     1   dir    qa
## 3:     2   mgr   dev
## 4:     2   mgr    qa
## 5:     2   mgr   ops
## 6:     3   dir   dev
## 7:     3   dir    qa
## 8:     3   dir   ops
## 9:     4    vp   dev

您从哪个软件包运行
ctab
?您从哪个软件包运行
ctab
library(data.table)
setDT(tmp2)
res <- tmp2[,index:=1:.N][,.(value=unlist(strsplit(roles,","))),.(index,level)]

##> res
##    index level value
## 1:     1   dir   dev
## 2:     1   dir    qa
## 3:     2   mgr   dev
## 4:     2   mgr    qa
## 5:     2   mgr   ops
## 6:     3   dir   dev
## 7:     3   dir    qa
## 8:     3   dir   ops
## 9:     4    vp   dev
dcast(res,level~value,value.var="value")

##    level  ops  qa dev
## 1:   dir    1   2   2
## 2:   mgr    1   1   1
## 3:    vp    0   0   1