Hi I have a dataframe similar to the one in the photo (left one) and I'm trying to obtain the format on the right one. How can I do it?
example dataset and what I would like to obtain
I know that is a stupid question, but I'm missing something..
How can I obtain that result? thank you very much!
I tried those one
long_data <- melt(data,id=c("Test"))
data.long <- data %>% pivot_longer(-Test, names_to = "variables", values_to = "value")
I also tried just
data_long=t(data)
library(dplyr)
library(tidyr)
df %>%
pivot_longer(-Test) %>%
pivot_wider(names_from = Test, values_from = value)
name Pre Post
<chr> <list> <list>
1 Q1 <dbl [4]> <dbl [4]>
2 Q2 <dbl [4]> <dbl [4]>
3 Q3 <dbl [4]> <dbl [4]>
or
library(dplyr)
library(tidyr)
df %>%
pivot_longer(-Test) %>%
pivot_wider(names_from = Test, values_from = value) %>%
unnest()
name Pre Post
<chr> <dbl> <dbl>
1 Q1 2 3
2 Q1 2 2
3 Q1 1 3
4 Q1 3 2
5 Q2 3 2
6 Q2 3 3
7 Q2 5 4
8 Q2 4 2
9 Q3 4 3
10 Q3 2 4
11 Q3 2 5
12 Q3 3 3
OR
library(dplyr) #>= 1.1.0
library(tidyr)
df %>%
pivot_longer(-Test) %>%
pivot_wider(names_from = Test, values_from = value) %>%
unnest() %>%
summarize(mean_pre = mean(Pre), mean_post = mean(Post), .by=name)
name mean_pre mean_post
<chr> <dbl> <dbl>
1 Q1 2 2.5
2 Q2 3.75 2.75
3 Q3 2.75 3.75
data:
df <- structure(list(Test = c("Pre", "Pre", "Pre", "Pre", "Post", "Post",
"Post", "Post"), Q1 = c(2, 2, 1, 3, 3, 2, 3, 2), Q2 = c(3, 3,
5, 4, 2, 3, 4, 2), Q3 = c(4, 2, 2, 3, 3, 4, 5, 3)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L))