如何替换表中选定列的 NA 值 [英] How to replace NA values in a table for selected columns

查看:30
本文介绍了如何替换表中选定列的 NA 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于替换 NA 值的帖子.我知道可以用以下内容替换下表/框架中的 NA:

There are a lot of posts about replacing NA values. I am aware that one could replace NAs in the following table/frame with the following:

x[is.na(x)]<-0

但是,如果我想将其限制为仅某些列怎么办?让我给你举个例子.

But, what if I want to restrict it to only certain columns? Let's me show you an example.

首先,让我们从一个数据集开始.

First, let's start with a dataset.

set.seed(1234)
x <- data.frame(a=sample(c(1,2,NA), 10, replace=T),
                b=sample(c(1,2,NA), 10, replace=T), 
                c=sample(c(1:5,NA), 10, replace=T))

给出:

    a  b  c
1   1 NA  2
2   2  2  2
3   2  1  1
4   2 NA  1
5  NA  1  2
6   2 NA  5
7   1  1  4
8   1  1 NA
9   2  1  5
10  2  1  1

好的,所以我只想将替换限制为a"和b"列.我的尝试是:

Ok, so I only want to restrict the replacement to columns 'a' and 'b'. My attempt was:

x[is.na(x), 1:2]<-0

和:

x[is.na(x[1:2])]<-0

这不起作用.

我的 data.table 尝试,其中 y<-data.table(x),显然永远不会工作:

My data.table attempt, where y<-data.table(x), was obviously never going to work:

y[is.na(y[,list(a,b)]), ]

我想在 is.na 参数中传递列,但这显然行不通.

I want to pass columns inside the is.na argument but that obviously wouldn't work.

我想在 data.frame 和 data.table 中执行此操作.我的最终目标是将 'a' 和 'b' 中的 1:2 重新编码为 0:1,同时保持 'c' 的原样,因为它不是逻辑变量.我有一堆专栏,所以我不想一个一个地做.而且,我只想知道如何做到这一点.

I would like to do this in a data.frame and a data.table. My end goal is to recode the 1:2 to 0:1 in 'a' and 'b' while keeping 'c' the way it is, since it is not a logical variable. I have a bunch of columns so I don't want to do it one by one. And, I'd just like to know how to do this.

您有什么建议吗?

推荐答案

您可以:

x[, 1:2][is.na(x[, 1:2])] <- 0

或者更好(恕我直言),使用变量名:

or better (IMHO), use the variable names:

x[c("a", "b")][is.na(x[c("a", "b")])] <- 0

在这两种情况下,1:2c("a", "b") 都可以替换为一个预定义的向量.

In both cases, 1:2 or c("a", "b") can be replaced by a pre-defined vector.

这篇关于如何替换表中选定列的 NA 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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