简体   繁体   English

用R中另一个数据框的值填写缺失值(NA)

[英]Fill in missing values (NAs) with values from another dataframe in R

How do I subset missing values in one dataframe with values from another? 如何将一个数据框中的缺失值与另一个数据框中的值子集化?

Let's say I have two datasets: 假设我有两个数据集:

dataset 1 shows the amount of food that is produced by a country each day. 数据集1显示了一个国家每天生产的食物数量。

 country         day     tonnes of food
## 1       china  1          6
## 2       china  1          NA
## 3       china  2          2
## 4       china  2          NA

dataset2 is the average amount of food by day 数据集2是每天的平均食物量

country         day     average tonnes of food
## 1       china  1          6
## 3       china  2          2

How can I fill in the NAs of dataset1 with the averages from dataset2. 如何用数据集2的平均值填写数据集1的NA。

Ie IF is.na(dataset1$tonnes) is TRUE then fill in with average for day from dataset2$averagetonnes 即如果IF is.na(dataset1$tonnes)为TRUE,则用数据dataset2$averagetonnes日期进行dataset2$averagetonnes

We can use join in data.table 我们可以在data.table使用join

library(data.table)
setDT(df1)[df2, on =c("country", "day")][is.na(tonnes.of.food), 
  tonnes.of.food:= average.tonnes.of.food][, average.tonnes.of.food:=NULL][]
#   country day tonnes.of.food
#1:   china   1              6
#2:   china   1              6
#3:   china   2              2
#4:   china   2              2

If I understand you correctly using the match function will solve your problem. 如果我正确理解您的信息,则可以使用match功能解决您的问题。 Data: 数据:

df1 <- data.frame(country=c(rep('china1',2),rep('china2',2)),day=c(1,1,2,2),tof = c(6,NA,2,NA),stringsAsFactors = F)
df2 <- data.frame(country=c('china1','china2'),day=c(1,2),atof = c(6,2),stringsAsFactors = F)
df1
  country day tof
#1  china1   1   6
#2  china1   1  NA
#3  china2   2   2
#4  china2   2  NA

This line will replace the NAs with the averages of the corresponding country of the second data.frame df2. 该行将用第二个data.frame df2对应国家/地区的平均值替换NA。 The match function results in a vector of positions of matches and [which(is.na(df1$tof))] selects the indices where there is a NA in the “tof” column. match函数产生match位置的向量, [which(is.na(df1$tof))]选择“ tof”列中不存在NA的索引。

df1$tof[is.na(df1$tof)] <- df2$atof[match(df1$country,df2$country)][which(is.na(df1$tof))]
df1
  country day tof
#1  china1   1   6
#2  china1   1   6
#3  china2   2   2
#4  china2   2   2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM