我有很多测量因素,例如:
measurement1Height;
[1] 176 177 180 181 177
Levels: 176 177 180 181
measurement2Height;
[1] 176 177 180 181 177
Levels: 176 177 180 181
measurement3Height;
[1] 176 177 180 181 176
Levels: 176 180 181
measurement1Weight;
[1] 73 79 85 85 80
Levels: 73 79 80 85
measurement2Weight;
[1] 75 80 84 85 80
Levels: 75 80 84 85
measurement3Weight;
[1] 74 79 85 84 76
Levels: 74 76 79 84 85
measurement1Height <- factor(c(176, 177, 180, 181, 177));
measurement2Height <- factor(c(176, 177, 180, 181, 177));
measurement3Height <- factor(c(176, 177, 180, 181, 176));
measurement1Weight <- factor(c(73, 79, 85, 85, 80));
measurement2Weight <- factor(c(75, 80, 84, 85, 80));
measurement3Weight <- factor(c(74, 79, 85, 84, 76));
我需要获取输出表,其中所有数据均按度量分组:
Measurement1 Measurement2 Measurement3
Height Weight Height Weight Height Weight
Person1 176 73 176 75 176 74
Person2 177 79 177 80 177 79
Person3 180 85 180 84 180 85
Person4 181 85 181 85 181 84
Person5 177 80 177 80 176 76
有没有办法快速做到?
答案 0 :(得分:1)
这是使用dplyr
的快速而肮脏的解决方案。
library(dplyr)
# your data
measurement1Height <- factor(c(176, 177, 180, 181, 177))
measurement2Height <- factor(c(176, 177, 180, 181, 177))
measurement3Height <- factor(c(176, 177, 180, 181, 176))
measurement1Weight <- factor(c(73, 79, 85, 85, 80))
measurement2Weight <- factor(c(75, 80, 84, 85, 80))
measurement3Weight <- factor(c(74, 79, 85, 84, 76))
# operations
data_names <- as.list(sort(paste0(paste0("measurement", 1:3), rep(c("Height", "Weight"), each=3))))
data <- lapply(data_names, get)
names(data) <- data_names
data <- data %>%
bind_rows() %>%
mutate(Person = paste("Person", 1:nrow(bind_rows(data)))) %>%
select(Person, everything())
# here's your new data
data
毕竟,我建议使用整数和数值而不是因数(取决于您的目标)。