在R中铸造数据帧

在R中铸造数据帧,r,dataframe,reshape,transpose,tidyr,R,Dataframe,Reshape,Transpose,Tidyr,我的数据如下所示: dput(head(dat, 10) structure(list(Label = c("Nuclear Blast", "Nuclear Blast", "Nuclear Blast", "Nuclear Blast", "Nuclear Blast", "Nuclear Blast", "Nuclear Blast", "Metal Blade Records", "Metal Bla

我的数据如下所示:

dput(head(dat, 10)
structure(list(Label = c("Nuclear Blast", "Nuclear Blast", "Nuclear Blast", 
                     "Nuclear Blast", "Nuclear Blast", "Nuclear Blast", "Nuclear Blast", 
                     "Metal Blade Records", "Metal Blade Records", "Metal Blade Records"
), Info = c("Germany", " +49 7162 9280-0 ", "active", " N/A ", 
        "1987", "\n\t\t\t\t\t\t\t\t\tAnstalt Records,\t\t\t\t\t\t\t\t\tArctic Serenades,\t\t\t\t\t\t\t\t\tCannibalised Serial Killer,\t\t\t\t\t\t\t\t\tDeathwish Office,\t\t\t\t\t\t\t\t\tEpica,\t\t\t\t\t\t\t\t\tGore Records,\t\t\t\t\t\t\t\t\tGrind Syndicate Media,\t\t\t\t\t\t\t\t\tNuclear Blast America,\t\t\t\t\t\t\t\t\tNuclear Blast Brasil,\t\t\t\t\t\t\t\t\tNuclear Blast Entertainment,\t\t\t\t\t\t\t\t\tRadiation Records,\t\t\t\t\t\t\t\t\tRevolution Entertainment\t\t\t\t\t      ", 
        "Yes", " 5737 Kanan Road #143\n\nAgoura Hills, California 91301 ", 
        "United States", " N/A ")), .Names = c("Label", "Info"), row.names = c(NA, 
                                                                               10L), class = "data.frame")
我如何重塑它,使它看起来像以下

  Label                 Var1            Var2            Var3      Var4   Var5    Var6                Var7
1 Nuclear Blast        Germany      +49 7162 9280-0     active    N/A    1987    Anstalt Records...  Yes 
2 Metal Blade Records  5737 Kanan.. United States       N/A
我意识到每个标签的行数不一致,但我可以稍后在excel或R中进行清理。

尝试以下方法:

library(data.table)
setDT(dat)

dat[, Col:= paste0('Var', 1:.N), by='Label']

dat = dcast.data.table(dat, Label ~ Col, value.var='Info')
试试这个:

library(data.table)
setDT(dat)

dat[, Col:= paste0('Var', 1:.N), by='Label']

dat = dcast.data.table(dat, Label ~ Col, value.var='Info')

这里有一个使用
dplyr/tidyr

library(dplyr)
library(tidyr)
dat %>% 
    group_by(Label) %>% #group by Label
    mutate(Col = paste0("Var", row_number())) %>% #create a sequence column
    spread(Col, Info) #spread to wide format

这里有一个使用
dplyr/tidyr

library(dplyr)
library(tidyr)
dat %>% 
    group_by(Label) %>% #group by Label
    mutate(Col = paste0("Var", row_number())) %>% #create a sequence column
    spread(Col, Info) #spread to wide format

为什么第8行
none
出现在转换数据的第4列到第3行?看不到模式。请更改预期输出以匹配您的示例为什么第8行
none
出现在转换数据的第4列第3行?看不到模式。请更改预期输出,使exampleWorks与提供的示例数据相匹配,而不是与原始数据相匹配。我在原始问题中提供了一个带有
dput
的可复制示例。使用提供的样本数据,但不使用原始数据。在最初的问题中,我提供了一个带有
dput
的可复制示例。