简体   繁体   English

使用NA的条件语句从数据框中的列填充值-R

[英]Infilling values from column in dataframe with conditional statement for NAs - R

I have a dataframe as follows: 我有一个数据框,如下所示:

Date        FLOW     Modelled   Infilled
01-01-1992  1.856    1.900      NA
02-01-1992  1.523    1.500      NA
03-01-1992  NA       2.400      NA    
04-01-1992  3.679    3.800      NA

I want to fill the Infilled column with FLOW values. 我想用FLOW值填充Infilled列。 Where there are "NA" values in the FLOW column of the time series I want to replace these NAs with values from the Modelled column. 在时间序列的“流量”列中有“ NA”值的地方,我想用“建模”列中的值替换这些NA。

Answer should look like this: 答案应如下所示:

Date        FLOW     Modelled   Infilled
01-01-1992  1.856    1.900      1.856
02-01-1992  1.523    1.500      1.523
03-01-1992  NA       2.400      2.400    
04-01-1992  3.679    3.800      3.679

I have a solution as follows in excel: 我在excel中有以下解决方案:

Infilled column   =IF((FLOW="NA"),Modelled,FLOW)

I have not yet found a solution online to help me programme this in R. The time series are pretty lengthy and I have multiple files to do this for, so a loop could be the most suitable solution. 我尚未找到在线解决方案来帮助我在R中进行编程。时间序列非常长,我需要执行多个文件,因此循环可能是最合适的解决方案。 I am relatively new to R and I can't figure this out. 我对R比较陌生,我无法弄清楚。 Help much appreciated! 帮助非常感谢!

You are looking for coalesce 您正在寻找coalesce

library(tidyverse)
dat%>%
   mutate(Infilled=coalesce(FLOW,Modelled))
        Date  FLOW Modelled Infilled
1 01-01-1992 1.856      1.9    1.856
2 02-01-1992 1.523      1.5    1.523
3 03-01-1992    NA      2.4    2.400
4 04-01-1992 3.679      3.8    3.679

In base R you can do: 在基数R中,您可以执行以下操作:

transform(dat,Infilled=ifelse(is.na(FLOW),Modelled,FLOW))
        Date  FLOW Modelled Infilled
1 01-01-1992 1.856      1.9    1.856
2 02-01-1992 1.523      1.5    1.523
3 03-01-1992    NA      2.4    2.400
4 04-01-1992 3.679      3.8    3.679

We can use base R 我们可以使用base R

dat$Infilled <- dat$FLOW
i1 <- is.na(dat$FLOW)
dat$Infilled[i1] <- dat$Modelled[i1]

Or with data.table 或与data.table

library(data.table)
setDT(dat)[, Infilled := FLOW][is.na(FLOW), Infilled := Modelled][]

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

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