2

I have a dataframe as follows:

ID c1 c 2 c3 c4
1 3 6
2 1
3 8 1 9
4 1 10

I'm looking for a way to add these numbers together as far left as possible. In the example, my desired output would be

ID c1 c 2 c3 c4
1 3 6
2 1
3 8 1 9
4 1 10

How would I be able to do that? The transferring could also be to new columns, not necessarily the existing ones. Thanks in advance!

1 Answers1

1

This function should do it:

mytable <- as.data.frame(t(apply(mytable,1, function(x) { return(c(x[!is.na(x)],x[is.na(x)]))})))