[英]How do I make this table into a data frame? I'm using R. Trying to get the ANOVA table for it
Here's what I have, but I don't think it's right:这是我所拥有的,但我认为它不正确:
gas.test <- data.frame(gas=c(0, 14, 12, 13, 11, 17, 0, 13, 11, 10, 14, 14, 0,
12, 12, 13, 13, 12, 0, 11, 12, 10, 9, 8, 0),
Test_Car = factor(rep(c("A", "B", "C", "D", "E"), each=5)),
Additive = factor(rep(c(1, 2, 3, 4, 5), 5)))
I am guessing you are after either getting the matrix out of gas.test or the other way around.我猜你是在从 gas.test 中取出矩阵之后,或者反过来。
Here are both options, using pivot_wider
and pivot_longer
.这里有两个选项,使用pivot_wider
和pivot_longer
。
library(tidyr)
gas.test %>%
pivot_wider(names_from = Test_Car, values_from = gas)
# A tibble: 5 x 6 Additive AB C DE <fct> <dbl> <dbl> <dbl> <dbl> <dbl> 1 1 0 17 14 13 12 2 2 14 0 14 13 10 3 3 12 13 0 12 9 4 4 13 11 12 0 8 5 5 11 10 12 11 0
mat %>% pivot_longer(!Additive, names_to = "Test_Cars", values_to = "gas")
# A tibble: 25 x 3 Additive Test_Cars gas <fct> <chr> <dbl> 1 1 A 0 2 1 B 17 3 1 C 14 4 1 D 13 5 1 E 12
using spread
and gather
functions.使用spread
和gather
功能。
mat <- gas.test %>% spread(key = Test_Car, value = gas)
gas.test <- mat %>% gather(key = Test_Car, value = gas, -Additive)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.