CsabaD
CsabaD

Reputation: 29

Moving numbers in a data frame

I have a data frame and I want to sort my data. I want to move the first numbers of the first row in the first "cell", the second number in the second, etc, no matters what is the header. I want to make this from the first row to the last.

Any suggestion?

Thanks

Here is the original df

Upvotes: 1

Views: 44

Answers (1)

akrun
akrun

Reputation: 886938

We can use apply to loop over the rows (MARGIN = 1) to subset the non-NA elements, followed by the NA elements, concatenate it (c(), transpose (t) the output and assign it to original dataset

df1[] <- t(apply(df1, 1, FUN = function(x) c(x[!is.na(x)], x[is.na(x)])))

Upvotes: 2

Related Questions