Hi I am having trouble with something in R. I'm trying to merge (combine?) two (factor) columns in a data frame. For each row, there is a value in only one of the columns and I want to combine them so that all the rows have a value. As a simplified example, suppose I've run the following code: df <- data.frame(x=c("a", "b", " ", " "), y=c(" ", " ", "q", " "), z=c(" ", " ", " ", "p"))
, I get the following data frame
x y
1 a
2 b
3 q
After the x and y columns are merged, The result would be
x y merged
1 a a
2 b b
3 q q
I have tried using df$merged = ifelse(df$x == " ", df$y, df$x)
, but it gives me these numbers. Any idea what they mean?
x y merged
1 a 2
2 b 3
3 q 2
All the other helpful information I have come across works well with numbers, but not characters. Am I on the right track with what I have tried so far?
It seems like such a simple problem but I have not been able to find a solution. Any help would be appreciated.
Thanks all.