在R中的数据框中查找,替换和丢弃值

时间:2021-09-01 16:18:35

I'm trying to figure out how to find, replace and discard values in dataframe in R. I'm using the built in Loblolly data set in R. I want to replace the height data recorded during the growth of seed 311 with NAs; and then discard all data from three-year-old trees.

我正在试图弄清楚如何在R中的数据框中查找,替换和丢弃值。我正在使用R中内置的Loblolly数据集。我想用NA替换种子311生长期间记录的高度数据;然后丢弃三年树木的所有数据。

I tried this, but it doesn't work for some reason:

我试过这个,但由于某种原因它不起作用:

for('311' in Loblolly$Seed) Loblolly$height <- 'NA'

In Matlab there is a nice find function, are there any analogs in R?

在Matlab中有一个很好的查找功能,R中是否有任何类似物?

1 个解决方案

#1


1  

You can use the ifelse() function for this task.

您可以使用ifelse()函数执行此任务。

data(Loblolly)
Loblolly$height <- ifelse(Loblolly$Seed == 311, NA, Loblolly$height) 

The above replaced height with NA for only Seed 311.

上面用种子311取代了NA的高度。

To make a new data frame excluding the 3 year old trees you can do:

要创建一个不包含3年树的新数据框,您可以执行以下操作:

Loblolly2 <- Loblolly[Loblolly$age != 3, ]

Below I print just rows 20-30 to show the results for Seed 311:

下面我只打印20-30行以显示种子311的结果:

Loblolly2[20:30, ]
   height age Seed
74  59.07  25  307
19  11.20   5  309
33  28.66  10  309
47  41.66  15  309
61  53.31  20  309
75  63.05  25  309
20     NA   5  311
34     NA  10  311
48     NA  15  311
62     NA  20  311
76     NA  25  311

#1


1  

You can use the ifelse() function for this task.

您可以使用ifelse()函数执行此任务。

data(Loblolly)
Loblolly$height <- ifelse(Loblolly$Seed == 311, NA, Loblolly$height) 

The above replaced height with NA for only Seed 311.

上面用种子311取代了NA的高度。

To make a new data frame excluding the 3 year old trees you can do:

要创建一个不包含3年树的新数据框,您可以执行以下操作:

Loblolly2 <- Loblolly[Loblolly$age != 3, ]

Below I print just rows 20-30 to show the results for Seed 311:

下面我只打印20-30行以显示种子311的结果:

Loblolly2[20:30, ]
   height age Seed
74  59.07  25  307
19  11.20   5  309
33  28.66  10  309
47  41.66  15  309
61  53.31  20  309
75  63.05  25  309
20     NA   5  311
34     NA  10  311
48     NA  15  311
62     NA  20  311
76     NA  25  311