在R中的数据帧中移动NAs [英] Move NAs within dataframe in R

查看:150
本文介绍了在R中的数据帧中移动NAs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据框:

  df<  -  structure(list(a = c(NA, 1,2L,3L,4L,5L,6L,7L,8L),b = c(NA,NA,NA,1L,2L,3L,4L,5L,6L,7L) NA,NA,1L,2L,3L,4L,5L,6L)),Names = c(a,b,d),row.names = c(NA,-10L) data.frame)

> df
abd
1 NA NA NA
2 NA NA NA
3 1 NA NA
4 2 1 NA
5 3 2 1
6 4 3 2
7 5 4 3
8 6 5 4
9 7 6 5
10 8 7 6

我想向上移动每列,并将NAs移动到数据框的底部:

 > df.out 
abd
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 NA
8 8 NA NA
9 NA NA NA
10 NA NA NA
pre>

更新以使我的问题更清晰..

 code> df<  -  structure(list(a = c(NA,NA,1,5,34,7,3,5,8,4),b = c(NA,
NA, NA,57,2,7,9,5,12,100),d = c(NA,NA,NA,NA,5,7,
2,8,2,5)), c(a,b,d),row.names = c(NA,-10L
),class =data.frame)

> df
abd
1 NA NA NA
2 NA NA NA
3 1 NA NA
4 5 57 NA
5 34 2 5
6 7 7 7
7 3 9 2
8 5 5 8
9 8 12 2
10 4 100 5

应该导致:

  abd 
1 1 57 5
2 5 2 7
3 34 7 2
4 7 9 8
5 3 5 2
6 5 12 5
7 8 100 NA
8 4 NA NA
9 NA NA NA
10 NA NA NA

似乎是一个简单的任务,但我坚持在哪里开始..你能帮助吗?

解决方案

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

 #以beetroot命名为第一个需要这个功能
beetroot < - function(x){
#count NA
num.na< - sum(is.na(x))
#删除NA
x < !is.na(x)]
#结束$ b结尾的NAs数$ bx< - c(x,rep(NA,num.na))
return(x)
}

#在数据框中的每列上应用beetroot
as.data.frame(lapply(df,beetroot))

它会计算NAs,删除NAs,并在数据框中的每列的底部粘贴NAs。


I have such a data frame:

df <- structure(list(a = c(NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), b = c(NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L), d = c(NA, NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L)), .Names = c("a", "b", "d"), row.names = c(NA, -10L), class = "data.frame")

> df
    a  b  d
1  NA NA NA
2  NA NA NA
3   1 NA NA
4   2  1 NA
5   3  2  1
6   4  3  2
7   5  4  3
8   6  5  4
9   7  6  5
10  8  7  6

I'd like to move up each column and move the NAs to the bottom of the data frame:

> df.out
    a  b  d
1   1  1  1
2   2  2  2
3   3  3  3
4   4  4  4
5   5  5  5
6   6  6  6
7   7  7 NA
8   8 NA NA
9  NA NA NA
10 NA NA NA

Update to make my questions clearer..

df <- structure(list(a = c(NA, NA, 1, 5, 34, 7, 3, 5, 8, 4), b = c(NA, 
NA, NA, 57, 2, 7, 9, 5, 12, 100), d = c(NA, NA, NA, NA, 5, 7, 
2, 8, 2, 5)), .Names = c("a", "b", "d"), row.names = c(NA, -10L
), class = "data.frame")

> df
    a   b  d
1  NA  NA NA
2  NA  NA NA
3   1  NA NA
4   5  57 NA
5  34   2  5
6   7   7  7
7   3   9  2
8   5   5  8
9   8  12  2
10  4 100  5

should result in:

    a   b  d
1   1  57  5
2   5   2  7
3  34   7  2
4   7   9  8
5   3   5  2
6   5  12  5
7   8 100 NA
8   4  NA NA
9  NA  NA NA
10 NA  NA NA

Seems like an easy task but I am stuck on where to start.. Can you help?

解决方案

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))

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

这篇关于在R中的数据帧中移动NAs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆