博客来源于我的语雀专栏:R 语言 · 语雀
更多内容同步更新请关注我的语雀:令平子 · 语雀
参考资料:部分已引用到各段落
r - Reshaping data.frame from wide to long format - Stack Overflow
#create data frame
df <- data.frame(index=c(1, 2, 3, 4, 5, 6),
group=c('a','b','c','d','e','f'),
var1=c(4, 4, 5, 4, 3, 2),
var2=c(1, 2, 4, 4, 6, 9),
var3=c(9, 9, 9, 5, 5, 3))
方法一:reshape2::melt
long <- reshape2::melt(df , id.vars = c('index','group'), variable.name = 'series')
# index group series value
# 1 1 a var1 4
# 2 2 b var1 4
# 3 3 c var1 5
其他等效代码:
melt(df, id.vars = 1:2, variable.name = "series")
melt(df, measure.vars = 3:5, variable.name = "series")
melt(df, measure.vars = c("var1","var2","var3"), variable.name = "series")
方法二:tidyr::pivot_longer
long <- tidyr::pivot_longer(df,cols = c("var1","var2","var3"), names_to = "series",values_to = "value")
long <- tidyr::pivot_longer(df,cols =-c('index','group'), names_to = "series",values_to = "value")
方法三:reshape
long <- reshape(df, direction = "long",varying = c("var1","var2","var3"),v.names = "value",idvar = c('index','group'))
# index group time value
# 1: 1 a 1 4
# 2: 2 b 1 4
# 3: 3 c 1 5
r - Convert data.frame column format from character to factor - Stack Overflow
一维数据框转long的核心就是将分类列设置成因子类型
df <- data.frame(index=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),
series=c('a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d'),
value=c(94,79,24,87,7,93,99,23,26,32,96,27,42,5,70,16,98,91,21,55))
# id series value
# 1 1 a 94
# 2 2 b 79
# 3 3 c 24
# 4 4 d 87
# 5 5 a 7
# 6 6 b 93
# 7 7 c 99
# 8 8 d 23
# 9 9 a 26
# 10 10 b 32
# 11 11 c 96
# 12 12 d 27
# 13 13 a 42
# 14 14 b 5
# 15 15 c 70
# 16 16 d 16
# 17 17 a 98
# 18 18 b 91
# 19 19 c 21
# 20 20 d 55
方法一:melt
library(data.table)
long <- melt(df, id.vars = c("id"), variable.name = "series")
方法二:reshape
long <- reshape(
df,
idvar = "series", # 对象标识符
v.names = "value", # 观测值
direction = "long") # 转换为长格式
方法三:单列、多列设置为factor 推荐
df$series<- as.factor(df$series) 或 df[, 'series'] <- as.factor(df[, 'series'])
多列设置
col_names <- c("series")
df[col_names] <- lapply(df[col_names] , factor)
r - Convert data from long format to wide format with multiple measure columns - Stack Overflow