0

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).

enter image description here

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)
Andrea
  • 41
  • 8
  • I see your logic but how do you want to merge these values? Are they numbers you want to add? text you want to merge together? – boski Jul 04 '19 at 07:32
  • What are the values you do not want? All NAs or something else? This would be easier for us with a toy example of your matrix/dataframe/tibble as input and what you want as output – Henry Jul 04 '19 at 07:33
  • @boski The values I want to add together in one column are all numbers. – Andrea Jul 04 '19 at 07:50
  • @Henry The only values I do not want are NA. I will try making a toy-example. – Andrea Jul 04 '19 at 07:50
  • 1
    @Cath, I think you are right, thank you – Andrea Jul 04 '19 at 07:53

1 Answers1

2

Package dplyr as a function coalesce perfect for this problem.

library(dplyr)

m <- matrix(NA, 4, 3)
diag(m) <- 1:3
m[4, 3] <- 4
m

Reduce(coalesce, as.data.frame(m))
#[1] 1 2 3 4

WIth the data in the question, but df_with_new_column created as an actual data.frame, it becomes:

df_with_new_column <- data.frame(dfcol1, dfcol2)

df_with_new_column$newcol <- Reduce(coalesce, as.data.frame(abcd))

df_with_new_column
#   dfcol1 dfcol2 newcol
#1     St1     S1      1
#2     St2     S2      2
#3     St3     S3      3
#4     St4     t4      1
#5     St5     S5      2
#6     St6     S6      3
#7     St7     S7      1
#8     St8     S8      2
#9     St9     S9      3
#10   St10    S10      1
#11   St11    S11      2
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66