有没有一种直接的方法可以根据 Y 列的值将 X 列中的值更改为 NA?
我有一个包含多个雷达数据的大型数据框。我只想将radarID“CLX”(Y列)的X列值(密度)更改为NA。
在 Excel 中,这将是一个简单的选择所有具有radarID =“CLX”的行,并用NA 替换所有密度值。
谢谢
有没有一种直接的方法可以根据 Y 列的值将 X 列中的值更改为 NA?
我有一个包含多个雷达数据的大型数据框。我只想将radarID“CLX”(Y列)的X列值(密度)更改为NA。
在 Excel 中,这将是一个简单的选择所有具有radarID =“CLX”的行,并用NA 替换所有密度值。
谢谢
In R also it is too simple, specially if you give us a reproducible example.
Here I create one :
dat <- data.frame( radarID = sample(c("CLX","OTHER"),10,rep=TRUE),
X = rnorm(10))
Then using transform
and ifelse
, you can do something like this :
transform(dat,X=ifelse(radarID=='CLX',NA,X))
radarID X
1 OTHER -1.03632461
2 OTHER 0.07634564
3 OTHER -0.33788092
4 CLX NA
5 CLX NA
6 CLX NA
7 OTHER 1.37040083
8 OTHER 0.50905176
9 CLX NA
10 OTHER -0.16086215
Of course this will work in a copy of your data. You need to assign it assign to dat if you want to have the change.
dat <- transform(dat,X=ifelse(radarID=='CLX',NA,X))
One way to do it is as follows. If df
is your data frame with two columsn x
and y
and you want to change x
based on y
such that when y
is less than 10
, x
should become NA
:
df[df$y < 10, 'x'] <- NA
You could also check out ?transform
and ?within
df = data.frame(Density = 1:4, radarID = c("blah", "boo", "CLX", "bam"))
df$Density[df$radarID == "CLX"] = NA