我有一个宽格式的数据框
structure(list(Id = structure(2:1, .Label = c("Jake", "Jim"
), class = "factor"), Q2.2 = c(NA, NA), Q2.4 = c(NA, NA), Q2.5 = c(NA,
NA), Q2.6 = c(NA, 4L), Q3.2 = c(NA, NA), Q3.4 = c(NA, NA), Q3.5 = c(NA,
NA), Q3.6 = c(4L, NA), Q4.2 = c(NA, NA), Q4.4 = c(2L, NA), Q4.5 = c(NA,
3L), Q4.6 = c(4L, NA), Q5.2 = c(NA, NA), Q5.4 = c(2L, NA), Q5.5 = c(NA,
NA), Q5.6 = c(NA, NA)), class = "data.frame", row.names = c(NA,
-2L))
我试图仅根据句点后的数字将其转换为长格式的数据框。因此,带有.2
的列中的所有值都应放在一个列中,而带有.4
的列中的所有值都应放在另一列中。每个后缀均如此。为了更清楚地解释,我想要看起来像这样的
答案 0 :(得分:2)
我认为OP的问题在评论中张贴的重塑问题上有更多的细节
您可以使用data.table
:
library(data.table)
setDT(dt) # Convert to data.table object
# Melt it into long format
dt <- melt(dt, id.vars = "Id")
dt[, variable := as.character(variable)]
dt[, value := as.character(value) ]
# make a substring of the type ID
type_id <- sapply((strsplit(dt$variable,
split = ".", fixed = TRUE)),
'[[', 2) # This is a subsetting function passed to sapply() - gets the second element of the output list
type_id <- paste0("type", type_id)
# Make a substring of the Quarter ID
quarter_id <- sapply((strsplit(dt$variable, split = ".", fixed = TRUE)), '[[', 1)
# Use data.table's dcast() to reshape the data again to get the shape you require
dt[, type_id := type_id]
dt[, quarter_id := quarter_id]
dcast(dt, Id + quarter_id ~ type_id, value.var = "value")
## Output
Id quarter_id type2 type4 type5 type6
1: Jake Q2 <NA> <NA> <NA> 4
2: Jake Q3 <NA> <NA> <NA> <NA>
3: Jake Q4 <NA> <NA> 3 <NA>
4: Jake Q5 <NA> <NA> <NA> <NA>
5: Jim Q2 <NA> <NA> <NA> <NA>
6: Jim Q3 <NA> <NA> <NA> 4
7: Jim Q4 <NA> 2 <NA> 4
8: Jim Q5 <NA> 2 <NA> <NA>
答案 1 :(得分:1)
@rove击败了我,但想发布另一种方法,只要我拥有80%即可:
library(reshape2)
x.melted <- melt(x, id.vars=c("Id"))
x.melted$quarter.id <- gsub("\\..*","", x.melted$variable)
x.melted$number.after.period <- gsub("Q[0-9]\\.", "", x.melted$variable)
x.casted <- dcast(x.melted, Id + quarter.id ~ number.after.period, )
colnames(x.casted)[3:ncol(x.casted)] <- paste("Type.",
colnames(x.casted)[3:ncol(x.casted)], sep="")
x.casted
Id quarter.id Type.2 Type.4 Type.5 Type.6
1 Jake Q2 NA NA NA 4
2 Jake Q3 NA NA NA NA
3 Jake Q4 NA NA 3 NA
4 Jake Q5 NA NA NA NA
5 Jim Q2 NA NA NA NA
6 Jim Q3 NA NA NA 4
7 Jim Q4 NA 2 NA 4
8 Jim Q5 NA 2 NA NA