I have a data frame with 563 columns.
Each column currently has rows of different length and NAs
For example:
col_1 col_2 col_3 ... col_563
"1" "3" "1" NA
"3" "4" "3" "2"
"2" "1" NA "3"
NA "2" NA "1"
"4" NA "2" NA
I want to sort all columns and omit the NAs so it looks like this:
col_1 col_2 col_3 ... col_563
"1" "1" "1" "1"
"2" "2" "2" "2"
"3" "3" "3" "3"
"4" "4"
To first sort the columns, I tried:
df_sorted <-df_final[order(df_final[,1:563] ),]
But the program crashed. Any help would be appreciated, thank you!
CodePudding user response:
your_df[] <- lapply(your_df, sort, na.last = TRUE)
your_df[is.na(your_df)] <- ''
col_1 col_2 col_3 col_563 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 5
CodePudding user response:
df[] <- apply(df, 2, sort, na.last = TRUE)
# col_1 col_2 col_3 col_4
# 1 1 1 1 1
# 2 2 2 2 2
# 3 3 3 3 3
# 4 4 4 NA NA
# 5 NA NA NA NA
Data
df <- data.frame(
col_1 = c(1, 3, 2, NA, 4),
col_2 = c(3, 4, 1, 2, NA),
col_3 = c(1, 3, NA, NA, 2),
col_4 = c(NA, 2, 3, 1, NA)
)