如何为选定列替换表中的NA值 [英] How to replace NA values in a table for selected columns
问题描述
关于替换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
或更好的(IMHO),请使用变量名称:
or better (IMHO), use the variable names:
x[c("a", "b")][is.na(x[c("a", "b")])] <- 0
在两种情况下,1:2
或c("a", "b")
都可以用预先定义的向量替换.
In both cases, 1:2
or c("a", "b")
can be replaced by a pre-defined vector.
这篇关于如何为选定列替换表中的NA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!