'Going from a dataframe with a factor column and numeric column to a dataframe with factor levels as columns and corresponding numeric values as rows
If I take the following two-column dataframe, with one column being a factor and the other being a numeric vector:
data <- data.frame(x=c("a","b","c","d","e","f","g","h"), y = c(1,2,3,3,2,1,5,6))
data$x <- as.factor(data$x)
How can I turn it into a new dataframe data2 where the factor levels of data$x are columns and the rows contain the corresponding numeric values from data$y, like so?
structure(list(a = 1, b = 2, c = 3, d = 3, e = 2, f = 1, g = 5,
h = 6), class = "data.frame", row.names = c(NA, -1L))
Solution 1:[1]
With base R, use rbind.data.frame:
d <- rbind.data.frame(data$y)
colnames(d) <- data$x
a b c d e f g h
1 1 2 3 3 2 1 5 6
With pivot_wider:
tidyr::pivot_wider(data, names_from = x, values_from = y)
a b c d e f g h
1 1 2 3 3 2 1 5 6
or with xtabs:
xtabs(y ~ ., data = data) |>
as.data.frame.list()
a b c d e f g h
1 1 2 3 3 2 1 5 6
Solution 2:[2]
Another possible solution, using data.table::transpose:
data.table::transpose(data, make.names = 1)
#> a b c d e f g h
#> 1 1 2 3 3 2 1 5 6
Solution 3:[3]
Another option using the sjmisc package:
library(sjmisc)
data %>%
rotate_df(cn = T)
# a b c d e f g h
#y 1 2 3 3 2 1 5 6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | PaulS |
| Solution 3 | AndrewGB |
