0

我正在尝试隐藏我的数据格式以便能够在其他软件中使用它。在我的情况下,我需要将 resp 的级别转换为单独的变量,同时保留每个 respID 的线索列表。我的数据如下

df<-structure(list(resp_ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), clues = structure(c(5L, 1L, 4L, 3L, 2L, 5L, 6L, 1L), .Label = c("clear", "elephants", 
    "green", "insects", "muddy", "salty"), class = "factor")), .Names = c("resp_ID", 
    "clues"), class = "data.frame", row.names = c(NA, -8L)) 
    df
      resp_ID     clues
    1       1     muddy
    2       1     clear
    3       1   insects
    4       2     green
    5       2 elephants
    6       2     muddy
    7       3     salty
    8       3     clear

#I want the resulting data to be like

 output<-structure(list(X1 = structure(c(3L, 1L, 2L), .Label = c("clear", 
"insects", "muddy"), class = "factor"), X2 = structure(c(2L, 
1L, 3L), .Label = c("elephants", "green", "muddy"), class = "factor"), 
    X3 = structure(c(3L, 2L, 1L), .Label = c("", "clear", "salty"
    ), class = "factor")), .Names = c("X1", "X2", "X3"), class = "data.frame", row.names = c(NA, 
-3L))

output

   X1        X2    X3
1   muddy     green salty
2   clear elephants clear
3 insects     muddy      
> 

我尝试使用(!!table(cbind(df[1],stack(df[1])[2]))),但我认为我在某处订购错误,也尝试使用但libary(caret)没有成功。

4

1 回答 1

2

一种想法是使用bind_colsfromdplyr如下,

library(dplyr)
l1 <- split(df$clues, df$resp_ID)
bind_cols(lapply(l1, `length<-`, max(length(l1)))) 

# A tibble: 3 × 3
#      `1`       `2`   `3`
#    <chr>     <chr> <chr>
#1   muddy     green salty
#2   clear elephants clear
#3 insects     muddy  <NA>

笔记

通过lukeA的恭维将长度设置为相等

于 2016-10-06T13:26:25.493 回答