samedi 1 février 2020

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.

In other words, I would like to replace all of the values in the City.x column except for the NA's.

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$ZIP = 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.

Aucun commentaire:

Enregistrer un commentaire