且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将NA移到数据框中每一列的末尾

更新时间:2022-12-12 09:14:22

在完全误解了问题之后,这是我的最终答案:

After completely misunderstanding the question, here is my final answer:

# named after beetroot for being the first to ever need this functionality
beetroot <- function(x) {
    # count NA
    num.na <- sum(is.na(x))
    # remove NA
    x <- x[!is.na(x)]
    # glue the number of NAs at the end
    x <- c(x, rep(NA, num.na))
    return(x)
}

# apply beetroot over each column in the dataframe
as.data.frame(lapply(df, beetroot))

它将对NA进行计数,删除NA,并在每个列的底部粘贴NA数据框。

It will count the NAs, remove the NAs, and glue NAs at the bottom for each column in the data frame.