From a dataframe like this:
df <- data.frame(row_name = c("sha1", "sha2", "edt1", "edt2", "perform1", "perform2"),
value = c(10, 20, 30, 40, 50, 60))
How is it possible to convert rows to columns to receive this result?
df <- data.frame(sha = c(10, 20), edt = c(30, 40), perform = c(50, 60))
>Solution :
You can do it first by separating the number from the words that you want to be the variable names. Then you can pivot_wider() on the names and then remove the variable that captured the number.
library(dplyr)
library(tidyr)
df <- data.frame(row_name = c("sha1", "sha2", "edt1", "edt2", "perform1", "perform2"),
value = c(10, 20, 30, 40, 50, 60))
df %>%
separate_wider_regex(row_name, c(name = "[^\\d]*", obs = "\\d")) %>%
pivot_wider(names_from = "name", values_from = "value") %>%
select(-obs)
#> # A tibble: 2 × 3
#> sha edt perform
#> <dbl> <dbl> <dbl>
#> 1 10 30 50
#> 2 20 40 60
Created on 2023-06-23 with reprex v2.0.2