on-internet
← Community

How do I go about pivoting/summarizing this data frame when the variables are spread between multiple columns?

Stack Overflow ·

I have a data frame that has the same variable in different columns, and a corresponding value for each variable. library(tidyverse) df <- data.frame( store = c("A", "B", "C"), popular.item.1 = c("a", "b", "b"), item.1.sold = c(5, 7, 1), popular.item.2 = c("b", "a", ""), item.2.sold = c(2, 3, NA) ) > as_tibble(df) # A tibble: 3 × 5 store popular.item.1 item.1.sold popular.item.2 item.2.sold <chr> <chr> <dbl> <chr> <dbl> 1 A a 5 "b" 2 2 B b 7 "a" 3 3 C b 1 "" NA I would like to turn this into a table where each item is a column with the variable of how many sold at each store below it: df2 <- d

Comments

Sign in or sign up to comment and upvote.

No comments yet.