读入样例数据(长类型olddata_long和宽类型olddata_wide各一个)
olddata_wide <- read.table(header=TRUE, text='
subject sex control cond1 cond2
1 M 7.9 12.3 10.7
2 F 6.3 10.6 11.1
3 F 9.5 13.1 13.8
4 M 11.5 13.4 12.9
')
# Make sure the subject column is a factor
olddata_wide$subject <- factor(olddata_wide$subject)
olddata_long <- read.table(header=TRUE, text='
subject sex condition measurement
1 M control 7.9
1 M cond1 12.3
1 M cond2 10.7
2 F control 6.3
2 F cond1 10.6
2 F cond2 11.1
3 F control 9.5
3 F cond1 13.1
3 F cond2 13.8
4 M control 11.5
4 M cond1 13.4
4 M cond2 12.9
')
# Make sure the subject column is a factor
olddata_long$subject <- factor(olddata_long$subject)
1,使用tidyr包
1.1 宽类型转长类型
library(tidyr)
# control:cond2 can also be written as "control, cond1, cond2"
data_long <- gather(olddata_wide, condition, measurement, control:cond2, factor_key=TRUE)
结果为
1.2 长类型转宽类型
data_wide <- spread(olddata_long, condition, measurement)
结果为
2,使用reshape2包
2.1 宽类型转长类型
data_long_rh2 <- melt(olddata_wide,
# ID variables - all the variables to keep but not split apart on
id.vars=c("subject", "sex"),
# The source columns
measure.vars=c("control", "cond1", "cond2" ),
# Name of the destination column that will identify the original
# column that the measurement came from
variable.name="condition",
value.name="measurement"
)
结果为
2.2 长类型转宽类型
data_wide_rh2 <- dcast(olddata_long, subject + sex ~ condition, value.var="measurement")
结果为