I am in the process of creating a new column with values from different datasets. Right Now I am at the point where I added all values at the right place in the dataset, but it is still spread over different columns. This has to become one column.
I want this (picture is easier than words imo).
So far I used pull
to get the values from the columns
then I used cbind
to knit them all together , so basically I have the same structure as starting out, only now with only the values that need to go in one row.
My thought for the next step was to stack
all the variables, then remove all the NA's (na.omit
or drop_na
) and then create a new column with mutate
something like df <- mutate(df, VRM = valueswithoutNA $ VRMvalues)
However, I get stuck at the stack
, because I get an error:
arguments imply differing number of rows: 511370, 0
Tips for getting around this error or to solve my problem in a different way?
edit: toy-example
dfcol1 <- c("St1", "St2", "St3", "St4", "St5", "St6",
"St7", "St8", "St9", "St10", "St11")
dfcol2 <- c("S1", "S2", "S3", "t4", "S5", "S6",
"S7", "S8", "S9", "S10", "S11")
df_with_new_column <- cbind(dfcol1, dfcol2)
aa <- c(1,2,3, NA,NA,NA,NA,NA,NA,NA,NA)
bb <- c(NA,NA,NA,1,2,3,NA,NA,NA,NA,NA)
cc <- c(NA,NA,NA,NA,NA,NA,1,2,3,NA,NA)
dd <- c(NA,NA,NA,NA,NA,NA,NA,NA,NA,1,2)
abcd <- cbind(aa,bb,cc,dd)