Combine 3 CSV columns into a variable

Hi there! The following should work:

library(tidyverse)

# Create dummy data

moviedata <- tribble(
  ~budget, ~screens, ~aggregate_followers,
  1200000, 750,      200,
  1500000, 1500,     320,
  8500000, 3000,     400,
  NA,      1800,     900,
  4400000, NA,       850,
  NA,      5000,     NA,
  3020000, 8500,     NA
)

# Use gather() to convert to long format

moviedf <- moviedata %>% 
  gather(`budget`, `screens`, `aggregate_followers`,
         key = "attribute", value = "value",
         na.rm = TRUE)

# View new dataframe

moviedf
#> # A tibble: 16 x 2
#>    attribute             value
#>  * <chr>                 <dbl>
#>  1 budget              1200000
#>  2 budget              1500000
#>  3 budget              8500000
#>  4 budget              4400000
#>  5 budget              3020000
#>  6 screens                 750
#>  7 screens                1500
#>  8 screens                3000
#>  9 screens                1800
#> 10 screens                5000
#> 11 screens                8500
#> 12 aggregate_followers     200
#> 13 aggregate_followers     320
#> 14 aggregate_followers     400
#> 15 aggregate_followers     900
#> 16 aggregate_followers     850

Created on 2018-10-01 by the reprex package (v0.2.0).

Notice that I created some dummy data to solve this. Generally, it's helpful to include a reprex (short for reproducible example) when you ask a question - this will ensure we're all looking at the same data and code. A guide for creating a reprex can be found here.

I hope this helps!

1 Like