R数据重组 - Melt2

时间:2015-10-30 00:20:09

标签: r melt

 V1      V2      V3      V4
 Control Control Unknown Unknown
 0       2       0       2
 66      10      90      70

行意味着: 实验, 时间, 长度分别为

你如何重构上面的数据看起来如下所示使用melt2?

V1      V2 V3
Control 0  66
Control 2  10
Unknown 0  90
Unknown 2  70

感谢您的时间。

p.s:我是新手程序员,试图学习R

1 个答案:

答案 0 :(得分:1)

您可以使用基数R(表示没有外部库/包),只需使用transpose

df <- data.frame(V1=c("Control", "0", "66"),  
                 V2=c("Control", "2", "10"),  
                 V3=c("Unknown", "0", "90"),
                 V4=c("Unknown", "2", "70"))    

reshape_df <- as.data.frame(t(df))
row.names(reshape_df) <- NULL

# CLEAN-UP
names(reshape_df)[1] <- "Experiment"
reshape_df$Experiment <- as.character(reshape_df$Experiment)
names(reshape_df)[2] <- "Time"
reshape_df$Time <- as.numeric(as.character(reshape_df$Time))
names(reshape_df)[3] <- "Length"
reshape_df$Length <- as.numeric(as.character(reshape_df$Length))

<强>输出

Experiment  Time   Length
Control     0      66
Control     2      10
Unknown     0      90
Unknwon     2      70