简体   繁体   English

用 R 中另一列的值替换特定列中的非 NA 值

[英]Replace non-NA values in a Certain Column with Values From Another Column in R

I'm trying to replace values in the City.x column of the mergedtable data frame with the values in the City.y column as long as there is not a NA in the City.y column.只要 City.y 列中没有 NA,我就会尝试用 City.y 列中的值替换合并表数据框 City.x 列中的值。

In other words, I would like to replace all of the values in the City.x column except for the NA's.换句话说,我想替换 City.x 列中除 NA 之外的所有值。

Here is the code that I have so far:这是我到目前为止的代码:

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)

Neither of the 2 lines of code at the very end are doing what I want.最后的两行代码都没有做我想要的。 The first one returns an error:第一个返回错误:

Error: unexpected '=' in:  
"mergedtable <- mergedtable %>%  
  mutate(mergedtable$City.x ="

The very last line turns the values in mergedtable$City.x into numbers, and I'm unsure where the numbers are coming from.最后一行将 mergetable$City.x 中的值转换为数字,但我不确定这些数字来自哪里。

It would be easier with coalesce coalesce会更容易

library(dplyr)
mergedtable2 <- mergedtable %>%
                     mutate(ZIP = coalesce(City.y, City.x))

In the OP's code, we need only to unquoted name ('ZIP') to create a new column在 OP 的代码中,我们只需要不带引号的名称('ZIP')就可以创建一个新列

mergedtable %>%
         mutate(ZIP = ifelse(!is.na(City.y), City.y, City.x))
                ^^^

Similarly相似地

food <- food %>%
              mutate(ZIP = ifelse("60627", "60827", ZIP))
                     ^^^

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

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