简体   繁体   中英

How to find and Replace String column values of a Data frame in R

In R i have a column in a dataframe which contains the City names. As shown in the below image.

enter image description here

This contains some erroneous data for example the Data N, Z, X needs to be replaced as "Others" and some city codes need to be replaced by their original names for example

OC, Okl City --> Oklahoma City
LA --> Los Angles
NW --> New York

When i tried doing this by using IF and ELSE IF statements inside a FOR Loop. I was very much Unsuccessful.

It will be of great help if someone can help me on this.

Thanks in Advance.

Here's a reproducible example using dplyr::case_when() that you can generalize to any number of conditions:

library(tidyverse)
d <- tibble(city = c("Oklahoma City","Los Angeles","OC","NY","Z","Z","X","N"))
d <- mutate(d, city = case_when(city %in% c("Z","X","N") ~ "Other", 
                                city == "Oklahoma City"  ~ "OKL",
                                city == "Los Angeles"    ~ "LA",
                                TRUE ~ city))
d


# A tibble: 8 x 1
  city 
  <chr>
1 OKL  
2 LA   
3 OC   
4 NY   
5 Other
6 Other
7 Other
8 Other

Make use of the revalue from the plyr package.

library(plyr)

df$city<-revalue(df$city,c("OC"="Oklahoma City",
                             "Okl City"="Oklahoma City",
                             "LA"="Los Angles",
                             "NW"="New York",
                             "Z"="Others",
                             "X"="Others",
                             "N"="Others"))

与以上答案中的@Rich相似但无条件case when可以使用。

library(tidyverse) d <- tibble(city = c("Oklahoma City","Los Angeles","OC","NY","Z","Z","X","N")) d <- mutate(d, city = case_when(!city %in% c("Oklahoma City", "Los Angeles" ) ~ "Other", city == "Oklahoma City" ~ "OKL", city == "Los Angeles" ~ "LA", TRUE ~ city))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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