【问题标题】:make levels of a variable into separate variables将变量的级别变成单独的变量
【发布时间】:2017-02-15 05:42:44
【问题描述】:

我正在尝试隐藏我的数据格式以便能够在其他软件中使用它。在我的情况下,我需要将 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),但没有成功。

【问题讨论】:

    标签: r dcast


    【解决方案1】:

    一个想法是使用bind_cols from dplyr如下,

    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>
    

    注意

    设置长度等于compliments of lukeA

    【讨论】:

    • 很好,它通过 temp length<-, n )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2020-07-30
    相关资源
    最近更新 更多