简体   繁体   中英

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.

Here are both options, using pivot_wider and 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.

mat <- gas.test %>% spread(key = Test_Car, value = gas)

gas.test <- mat %>% gather(key = Test_Car, value = gas, -Additive)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM