Reputation: 578
My data look like the following:
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")
How do I reshape it so it looks like the following?
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
I realize the number of rows for each label is inconsistent but I can clean that up later in excel or R.
Upvotes: 0
Views: 68
Reputation: 887128
Here is an option using 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
Upvotes: 1
Reputation: 1966
Try this:
library(data.table)
setDT(dat)
dat[, Col:= paste0('Var', 1:.N), by='Label']
dat = dcast.data.table(dat, Label ~ Col, value.var='Info')
Upvotes: 3