Combine 3 CSV columns into a variable

Hi
I tried to get values of 3 columns from a CSV and combine into a variable,but the output comes as combination of 3 and not as a single column.
EXample:

x<-c(1:5)
y<-c(6:10)
z<-c(11:15)

w<-c(x,y,z)

output

1  6 11
2  7  12
3  8  13
4  9  14
5  10 15

I want output like:1 2 3 4 5 6 7 8 9 10
Can someone help?
Following is the actual code:

       x<-subset(moviedata$budget, !is.na(moviedata$budget))
	y<-subset(moviedata$screens, !is.na(moviedata$screens))
	z<-subset(moviedata$aggregate_followers, !is.na(moviedata$aggregate_followers))
	moviedatasub<-as.data.frame(cbind(x,y,z))

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