I have a dataframe which has the following form
ID1 | VALUE |ID2 | VALUE | ID3 | VALUE
111 | one |333 | three | 555 | five
222 | two | | NA | 666 | six
Note that the number of values/rows in each column varies (NAs/empty values).
I want the result to look like this:
ID | VALUE
111 | one
222 | two
333 | three
555 | five
666 | six
I tried the following:
x = as.matrix(df)
x= matrix(x,ncol = 2)
This process mixes IDs and VALUEs for some columns. My guess is that this is because of NA and NULL values.
ID | VALUE
111 | one
222 | two
333 | three
five| 555
six | 666
Could you please suggest a quick solution(s) to my problem?
Elegant Solutions
Thanks to nongkrong and akrun for answers. I don't know about speed, but the most elegant solutions that got the job done are the following:
my_final = data.frame(VALUE=unlist(my_data[c(TRUE,FALSE)]), ID=unlist(my_data[c(FALSE, TRUE)]))
my_final = setNames(data.frame(lapply(split(as.list(my_data), c(F, T)), unlist)), c("VALUE", "ID"))
Also to omit empty rows:
my_final_trimmed = my_final[complete.cases(my_final), ]