[英]Turning columns in a data.frame (or vectors) into factors
I have a recurrent problem. 我经常遇到问题。 I often have multiple vectors or columns in a data.frame representing conditions.
我经常在data.frame中有多个表示条件的向量或列。 For example:
例如:
condition_1 condition_2 condition_3
5.3 2.6 1.2
25.5 2.2 1.4
13.1 0.1 9.2
...
Often I want to compare these conditions using an ANOVA. 通常,我想使用方差分析比较这些条件。 However, most ANOVA functions need the data to be specified as factors, like this:
但是,大多数ANOVA函数需要将数据指定为因素,例如:
value condition
5.3 condition_1
25.5 condition_1
13.1 condition_1
2.6 condition_2
2.2 condition_2
0.1 condition_2
1.2 condition_3
1.4 condition_3
9.2 condition_3
...
Is there a fast and easy way in R for converting from the former to the latter formatting? R中是否有快速简便的方法将前一种格式转换为后一种格式?
Sure. 当然。 You can use
stack
. 您可以使用
stack
。 It's not necessarily "fast" but it sure is easy. 它不一定“快速”,但肯定很容易。
stack(df)
# values ind
# 1 5.3 condition_1
# 2 25.5 condition_1
# 3 13.1 condition_1
# 4 2.6 condition_2
# 5 2.2 condition_2
# 6 0.1 condition_2
# 7 1.2 condition_3
# 8 1.4 condition_3
# 9 9.2 condition_3
sapply(stack(df), class)
# values ind
# "numeric" "factor"
where df
is df
在哪里
structure(list(condition_1 = c(5.3, 25.5, 13.1), condition_2 = c(2.6,
2.2, 0.1), condition_3 = c(1.2, 1.4, 9.2)), .Names = c("condition_1",
"condition_2", "condition_3"), class = "data.frame", row.names = c(NA,
-3L))
Alternate approach with melt
from reshape2
: 从
reshape2
melt
替代方法:
dat <- read.table(text="condition_1 condition_2 condition_3
5.3 2.6 1.2
25.5 2.2 1.4
13.1 0.1 9.2", stringsAs=FALSE, header=TRUE)
library(reshape2)
dat_m <- melt(dat)
dat_m
## variable value
## 1 condition_1 5.3
## 2 condition_1 25.5
## 3 condition_1 13.1
## 4 condition_2 2.6
## 5 condition_2 2.2
## 6 condition_2 0.1
## 7 condition_3 1.2
## 8 condition_3 1.4
## 9 condition_3 9.2
str(dat_m)
## 'data.frame': 9 obs. of 2 variables:
## $ variable: Factor w/ 3 levels "condition_1",..: 1 1 1 2 2 2 3 3 3
## $ value : num 5.3 25.5 13.1 2.6 2.2 0.1 1.2 1.4 9.2
Or using the new tidyr
package 或使用新的
tidyr
包
library(tidyr)
gather(dat, condition, value, condition_1:condition_3)
# condition value
# 1 condition_1 5.3
# 2 condition_1 25.5
# 3 condition_1 13.1
# 4 condition_2 2.6
# 5 condition_2 2.2
# 6 condition_2 0.1
# 7 condition_3 1.2
# 8 condition_3 1.4
# 9 condition_3 9.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.