![](/img/trans.png)
[英]R: Identify non-NA values from one column and create dataframe with values from another column based rows selected
[英]Replace non-NA values in a Certain Column with Values From Another Column in R
只要 City.y 列中沒有 NA,我就會嘗試用 City.y 列中的值替換合並表數據框 City.x 列中的值。
換句話說,我想替換 City.x 列中除 NA 之外的所有值。
這是我到目前為止的代碼:
library(tidyverse)
library(dplyr)
# Import food data
food <-
read_csv(file = 'https://s3.amazonaws.com/notredame.analytics.data/inspections.csv',
col_names=c("ID",
"DBAName",
"AKAName",
"License",
"FacilityType",
"Risk",
"Address",
"City",
"State",
"ZIP",
"InspectionDate",
"InspectionType",
"Results",
"Violations",
"Latitude",
"Longitude",
"Location"),
col_types = "icccffcfffcffcddc",
skip = 1)
# Change InspectionDate from character type to datetime type
food$InspectionDate <- strptime(food$InspectionDate, "%m/%d/%Y")
#Import zipcode data
zipcode <-
read_csv('https://s3.amazonaws.com/notredame.analytics.data/zipcode.csv',
col_names = c("ZIP",
"City",
"State",
"Latitude",
"Longitude"),
skip = 1)
# Convert ZIP, City, and State from character type to factor type
zipcode$ZIP <- as.factor(zipcode$ZIP)
zipcode$City <- as.factor(zipcode$City)
zipcode$State <- as.factor(zipcode$State)
#Correct zip codes (told these were incorrect)
food <- food %>%
mutate(food$ZIP = ifelse("60627", "60827", ZIP))
#Create merged table from food and zipcode tables
mergedtable <- merge(x=food,y=zipcode,by="ZIP",all.x=TRUE)
#new_DF <- mergedtable[is.na(mergedtable$ZIP),]
mergedtable <- mergedtable %>%
mutate(mergedtable$City.x = ifelse(!is.na(mergedtable$City.y), mergedtable$City.y, mergedtable$City.x))
mergedtable$City.x <- ifelse(!is.na(mergedtable$City.y), mergedtable$City.y, mergedtable$City.x)
最后的兩行代碼都沒有做我想要的。 第一個返回錯誤:
Error: unexpected '=' in:
"mergedtable <- mergedtable %>%
mutate(mergedtable$City.x ="
最后一行將 mergetable$City.x 中的值轉換為數字,但我不確定這些數字來自哪里。
coalesce
會更容易
library(dplyr)
mergedtable2 <- mergedtable %>%
mutate(ZIP = coalesce(City.y, City.x))
在 OP 的代碼中,我們只需要不帶引號的名稱('ZIP')就可以創建一個新列
mergedtable %>%
mutate(ZIP = ifelse(!is.na(City.y), City.y, City.x))
^^^
相似地
food <- food %>%
mutate(ZIP = ifelse("60627", "60827", ZIP))
^^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.