If I have a data set like follows
Rater1 10 a
Rater2 12 b
Rater2 5 c
Rater1 7 d
How should I code R to shift the data into a format like in two columns
Rater1 Rater2
10 12
7 5
a b
d c
to that one
CodePudding user response:
You could do the following:
as.data.frame(lapply(split(df, df$Rater), \(d) c(d$v1,d$v2)))
Output:
Rater1 Rater2
1 10 12
2 7 5
3 a b
4 d c
Input:
df = structure(list(Rater = c("Rater1", "Rater2", "Rater2", "Rater1"
), v1 = c(10, 12, 5, 7), v2 = c("a", "b", "c", "d")), class = "data.frame", row.names = c(NA,
-4L))
CodePudding user response:
Note that your final dataframe has columns with both numbers and characters in them and since a column can only have one class all the number would turn to characters.
We can do this with the help of dplyr
and tidyr
libraries. First we change the columns to character, get the data in long format, create a row number column to uniquely identify each row and finally get the data in wide format.
library(dplyr)
library(tidyr)
df %>%
mutate(across(.fns = as.character)) %>%
pivot_longer(cols = -Rater) %>%
group_by(Rater) %>%
mutate(name = row_number()) %>%
ungroup %>%
pivot_wider(names_from = Rater, values_from = value) %>%
select(-name)
# Rater1 Rater2
# <chr> <chr>
#1 10 12
#2 a b
#3 7 5
#4 d c