将变量的级别变为单独的变量

时间:2016-10-06 12:57:54

标签: r dcast

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

1 个答案:

答案 0 :(得分:2)

一个想法是使用gsub("^[^-]+-|_[^_]+$", "", rownames(df)) 中的v1 <- c("URS000075AF9C-snoRNA_GTATGTGTGGACAGCACTGAGACTGAGTCT", "URS000075B029-snRNA_AACTCTGAGTCTTAAGCTAATTTTTTGAGGCCTTGTTCCGACA", "URS000075B029-snRNA_ATTTCCGTGGAGAGGAACAACTCTGAGTCTTAAGCTAATTT", "URS000075B0E3-lncRNA_GTAAGGGGCAGTAAG", "URS000075B261-precursor_RNA_CTTTCTATGCTCCTGTTCTGC", "URS000075B2ED-lncRNA_CACTCAGGACCCACC") ,如下所示,

bind_cols

注意

将长度设置为compliments of lukeA