R 将NULL替换为不同ID的同一列中的值';s
在数据框的一列中,我有一些空单元格。数据如下所示:R 将NULL替换为不同ID的同一列中的值';s,r,replace,null,R,Replace,Null,在数据框的一列中,我有一些空单元格。数据如下所示: LoanID PaymentMonth Country 112345 201301 {null} 112345 201402 {null} 112345 201403 UK 234567 201301 US 234567 201302 {null} 234567 201303 {null} 我需要为不同的贷款id替换null。期望的结果是这
LoanID PaymentMonth Country
112345 201301 {null}
112345 201402 {null}
112345 201403 UK
234567 201301 US
234567 201302 {null}
234567 201303 {null}
我需要为不同的贷款id替换null。期望的结果是这样的
LoanID PaymentMonth Country
112345 201301 UK
112345 201402 UK
112345 201403 UK
234567 201301 US
234567 201302 US
234567 201303 US
如何解决这个问题?假设'Country'是
字符
类,{null}
是字符串,我们可以用NA
替换它,然后使用zoo
中的NA.locf
用相邻的非NA元素替换缺少的值
library(zoo)
df1$Country[df1$Country=="{null}"] <- NA
df1$Country <- with(df1, ave(Country, LoanID, FUN = function(x)
na.locf(na.locf(x, na.rm = FALSE), fromLast=TRUE)))
df1
# LoanID PaymentMonth Country
#1 112345 201301 UK
#2 112345 201402 UK
#3 112345 201403 UK
#4 234567 201301 US
#5 234567 201302 US
#6 234567 201303 US
图书馆(动物园)
df1$Country[df1$Country==“{null}”]带tidyverse
:
library(tidyr)
library(dplyr)
df %>%
mutate(Country = case_when(Country == '{null}' ~ NA_character_,
TRUE ~ Country)) %>%
group_by(LoanID) %>%
fill(Country, .direction = 'up') %>%
fill(Country, .direction = 'down')
#> Source: local data frame [6 x 3]
#> Groups: LoanID [2]
#>
#> LoanID PaymentMonth Country
#> <int> <int> <fctr>
#> 1 112345 201301 UK
#> 2 112345 201402 UK
#> 3 112345 201403 UK
#> 4 234567 201301 US
#> 5 234567 201302 US
#> 6 234567 201303 US
您可以只使用base R:ave(df$Country,df$LoanID,FUN=function(x){i=x=“{null}”;x[i]@docendodiscimus抱歉,我没有考虑group by,因为示例给出了与预期相同的输出,这正是我希望更新的内容,以解决标题中的主要问题
df1 <- structure(list(LoanID = c(112345L, 112345L, 112345L, 234567L,
234567L, 234567L), PaymentMonth = c(201301L, 201402L, 201403L,
201301L, 201302L, 201303L), Country = c("{null}", "{null}", "UK",
"US", "{null}", "{null}")), .Names = c("LoanID", "PaymentMonth",
"Country"), class = "data.frame", row.names = c(NA, -6L))
library(tidyr)
library(dplyr)
df %>%
mutate(Country = case_when(Country == '{null}' ~ NA_character_,
TRUE ~ Country)) %>%
group_by(LoanID) %>%
fill(Country, .direction = 'up') %>%
fill(Country, .direction = 'down')
#> Source: local data frame [6 x 3]
#> Groups: LoanID [2]
#>
#> LoanID PaymentMonth Country
#> <int> <int> <fctr>
#> 1 112345 201301 UK
#> 2 112345 201402 UK
#> 3 112345 201403 UK
#> 4 234567 201301 US
#> 5 234567 201302 US
#> 6 234567 201303 US
df <- read.table(text = 'LoanID PaymentMonth Country
112345 201301 {null}
112345 201402 {null}
112345 201403 UK
234567 201301 US
234567 201302 {null}
234567 201303 {null}', header = T, stringsAsFactors = F)
df <- read.table(text = 'LoanID PaymentMonth Country
112345 201301 {null}
112345 201402 {null}
112345 201403 UK
234567 201301 US
234567 201302 {null}
234567 201303 {null}', header = T, na.string = '{null}')
df %>%
group_by(LoanID) %>%
fill(Country, .direction = 'up') %>%
fill(Country, .direction = 'down')