简体   繁体   English

使用R中不同变量的值填充变量中的NA值

[英]Populate the NA values in a variable with values from a different variables in R

I have data which looks like this 我有看起来像这样的数据

Linking <- data.frame(
ID = c(round((runif(20, min = 10000, max = 99999)), digits = 0), NA, NA, NA, NA),
PSU = c(paste("A", round((runif(20, min = 10000, max = 99999)), digits = 0), sep = ''), NA, NA, NA, NA),
qtr = c(rep(1:10, 2), NA, NA, NA, NA)
)

Linking$Key <- paste(Linking$ID, Linking$PSU, Linking$qtr, sep = "_")
Linking$Key[c(21:24)] <- c("87654_A15467_1", "45623_A23456_2", "67891_A12345_4", "65346_A23987_7")

What I want to do is populate the NA values for ID, PSU, and qtr from the information from "Key", but only for the rows with NA values. 我想做的是根据“键”中的信息填充ID,PSU和qtr的NA值,但仅适用于具有NA值的行。

Does anyone know how to do this? 有谁知道如何做到这一点?

This code does what I want, but it does it for all values of each variable. 这段代码实现了我想要的功能,但是它对每个变量的所有值都做了。 I want to do this just for rows where the values are NA. 我只想对数值为NA的行执行此操作。

Linking2 <- Linking
Linking2$ID <- substr(Linking$Key,1,5)
Linking2$PSU <- substr(Linking$Key,7,12)
Linking2$qtr <- substr(Linking$Key, 14,15)

这里的基本思想是使用逻辑索引向量进行分配。

Linking$ID[is.na(Linking$ID)] <- substr(Linking$Key,1,5)[is.na(Linking$ID)]

You can also use tidyr::separate with dplyr::coalesce to separate values from Key and fill NA values in the first three columns. 您也可以使用tidyr::separatedplyr::coalesce来从Key分离值,并在前三列中填充NA值。

library(tidyverse);
Linking %>%
    separate(Key, into = paste0("tmp", 1:3), sep = "_") %>%
    mutate(ID = coalesce(tmp1), PSU = coalesce(tmp2), qtr = coalesce(tmp3)) %>%
    select(-tmp1, -tmp2, -tmp3);
#      ID    PSU qtr
#1  56421 A20914   1
#2  30912 A97582   2
#3  97547 A73397   3
#4  28277 A35255   4
#5  45107 A14867   5
#6  91099 A26196   6
#7  21117 A69721   7
#8  69065 A34741   8
#9  28781 A96847   9
#10 26098 A93942  10
#11 12680 A15705   1
#12 35231 A68219   2
#13 70556 A47693   3
#14 98281 A55697   4
#15 52687 A95023   5
#16 46129 A95540   6
#17 48721 A78827   7
#18 77989 A36778   8
#19 11757 A29458   9
#20 16575 A78892  10
#21 87654 A15467   1
#22 45623 A23456   2
#23 67891 A12345   4
#24 65346 A23987   7

暂无
暂无

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

相关问题 根据 R 中不同行的其他变量值的组合计算不同行变量值的差异 - Calculate difference of variable values from different rows based on combination of other variables' values from different rows in R 在 R 中从多列创建一个只有非 NA 值的变量 - Create a variable with only non NA values in R from multiple columns R 上 Na 值(数据框和变量)的百分比 - Percentuage on Na Values (Dataframe and Variables) on R 使用R,如果变量A或B中的值均为“ NA”,则改为使用变量C和D中的值? - Using R, if values in either variable A or B are 'NA', to then use values in variables C and D instead? 用不同的值填充 r 中的数据帧 - Populate a dataframe in r with different values 将不同变量的值存储在一个变量中 - Store values from different variables in one variable 如果另一个变量等于R中的设置值,如何使用来自不同变量的值创建新变量? - How to create a new variable with values from different variables if another variable equals a set value in R? 如果另一个变量等于R中的设置值,如何使用来自不同变量的值创建新变量? - How to create a new variable with values from different variables if another variable equals a set value in R? 如何根据 R dataframe 中的列将 NA 值替换为不同的值? - How to replace NA values with different values based on column in R dataframe? 如果焦点变量为NA,则在R中创建汇总变量,其值为“替代”变量 - Creation of summary variable in R with values of “substitution” variable if focal variable is NA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM