R:循环检查所有应用响应以创建虚拟指示器

smc*_*ary 4 r list

我从我想要处理的调查中找到了"检查所有适用项"的项目.数据来自一个字符串变量,其中响应者所做的每个选择都被编码到同一个变量中.受访者可以从21个选项列表中进行选择,这些选项都适用于他们.我想创建一组21个虚拟变量,表明是/否,受访者是否选择了特定选项.

三个示例响应是:

id  x 
1   3, 13
2   1, 3, 8, 9, 11, 13
3   1, 9
...
Run Code Online (Sandbox Code Playgroud)

我想要的是:

id  x                   x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13   
1   3, 13                0  0  1  0  0  0  0  0  0   0   0   0   1 
2   1, 3, 8, 9, 11, 13   1  0  1  0  0  0  0  1  1   0   1   0   1
3   1, 9                 1  0  0  0  0  0  0  0  1   0   0   0   0
...
Run Code Online (Sandbox Code Playgroud)

在我尝试这样做时,我已经将一个id变量和响应变量读入一个列表jp,以便每个受访者都有一个id jp[[1]]和他/她的响应jp[[2]]:

> jp[[2]][1:3]
[1] "3, 13                                                                     "
[2] "1, 3, 8, 9, 11, 13                                                        "
[3] "1, 9                                                                      "
Run Code Online (Sandbox Code Playgroud)

然后我通过strsplit逗号清理它们并将其放入jp[[4]]:

> jp[[4]][1:3]
[[1]]
[1] "3"  "13"

[[2]]
[1] "1"  "3"  "8"  "9"  "11" "13"

[[3]]
[1] "1" "9"
Run Code Online (Sandbox Code Playgroud)

我在所有列表元素中找到了唯一值:

> taught <- as.character(sort(as.numeric(unique(unlist(jp[[4]])))))
> taught
 [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12"  "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "256"
Run Code Online (Sandbox Code Playgroud)

通过一些试验和错误,我发现我可以按如下方式处理每个受访者的选择:

sapply(jp[[4]], function(x) any(x == "1"))
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常:

> table(sapply(jp[[4]], function(x) any(x == "1")))

FALSE  TRUE 
 9404  1891 
Run Code Online (Sandbox Code Playgroud)

这是我期望的普遍性.

但是,因为每个受访者都可以拥有0-21个响应(子列表元素),所以我认为我需要遍历每个受访者子列表中的每个唯一响应,并将结果写入新的列表元素.

我希望采用list元素jp[[4]],清理响应的位置和循环遍历'teach'的每个元素,以查看是否存在于每个响应者子列表中.

bla <- function(dt, lst) {
for (i in 1:length(lst)) {
            subs <- list()
            # apply function on each part, by row
            subs[[i]] <- sapply(dt, function(x) any(x == taught[i]))
    }  
    return(subs)
    }

bla(jp[[4]], taught)
Run Code Online (Sandbox Code Playgroud)

不幸的是,它似乎只适用于'teach'中的最后一个(21或'256')元素,并且不保存到我在函数中定义的'subs'列表中.

> table(bla(jp[[4]], taught)[21])

FALSE  TRUE 
10645   650 

> table(sapply(jp[[4]], function(x) any(x == "256")))

FALSE  TRUE 
10645   650 
Run Code Online (Sandbox Code Playgroud)

建议欢迎.谢谢.

Mai*_*ura 5

,作为数据集中的分隔符会产生问题.如果你用其他角色替换它-,那么它将使它更容易使用.假设你可以做到这一点,那么这应该工作.

tally<-function(df)
{
#create a data.frame with 23 columns, one for id, one for original x and 21 for responses   
response_table=data.frame(matrix(nrow=1,ncol=23))
names(response_table)=c("id","x",paste("x",1:21,sep=""))
response_table$id=df$id
response_table$x=df$x
response_table[,3:23]=0 
# Change the - to whatever separator you use
response_table[,as.numeric(unlist(str_split(df$x,'-')))+2]=1
return(response_table)
}



library(stringr)
test_data=data.frame(id=1:3,x=c("3-13","1-3-8-9-11-13","1-9"))

> test_data
  id             x
1  1          3-13
2  2 1-3-8-9-11-13
3  3           1-9
responses=ddply(test_data, .(id), tally)



> responses
  id             x x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21
1  1          3-13  0  0  1  0  0  0  0  0  0   0   0   0   1   0   0   0   0   0   0   0   0
2  2 1-3-8-9-11-13  1  0  1  0  0  0  0  1  1   0   1   0   1   0   0   0   0   0   0   0   0
3  3           1-9  1  0  0  0  0  0  0  0  1   0   0   0   0   0   0   0   0   0   0   0   0
Run Code Online (Sandbox Code Playgroud)