替换R中相同列和相同数据集中的值

时间:2017-11-15 16:21:27

标签: r

我有这样的数据集:

Name|Material |Project  
John|LS       |222  
Jack|Limestone|111  
Job |LI       |121  

我想用材质栏中的Limestone替换LS和​​LI 我试过这个LS:

ds$Material[ds$material %in% grep("^LS^",ds$Material,ignore.case = TRUE)]<-"Limestone" 

但该栏目中还有一堆LS。有什么想法吗?

4 个答案:

答案 0 :(得分:2)

您还可以使用gsub()LILS替换为Limestone

ds$Material <- gsub("LS|LI", "Limestone", ds$Material)
#   Name Materials Project
# 1 John Limestone     222
# 2 Jack Limestone     111
# 3  Job Limestone     121

答案 1 :(得分:1)

您的数据框

f1
ds <- 
    read.table(text = "Name Material Project
                        John LS        222  
                        Jack Limestone 111  
                        Job  LI        121  
                      ", header = TRUE, stringsAsFactors = FALSE)

对仅找到> ds Name Material Project 1 John LS 222 2 Jack Limestone 111 3 Job LI 121 的列进行子集。

LS

结果

ds$Material[grep("^L[SI]$", ds$Material, ignore.case = TRUE)] <- "Limestone"

在dplyr:

> ds
  Name  Material Project
1 John Limestone     222
2 Jack Limestone     111
3  Job        LI     121

答案 2 :(得分:1)

使用dplyr

df <- tibble(Name = c("John", "Jack", "Job"), Material = c("LS", "Limestone", "LI"), Project = c(222, 111, 121))

df %>% mutate(Material = stringr::str_trim(Material)) %>%  
        mutate(Material = case_when(
                              Material %in% c('LS', 'LI') ~ 'Limestone',
                              TRUE ~ Material
                              )
              )

答案 3 :(得分:1)

使用基础R,您可以使用ifelse

# Reproducible example
data <- read.table(textConnection("Name|Material |Project  
                                   John|LS       |222  
                                   Jack|Limestone|111  
                                   Job |LI       |121  "),
                   sep = "|", row.names = NULL, header = TRUE,
                   stringsAsFactors = FALSE, strip.white = TRUE)
data$Material <- ifelse(data$Material %in% c("LS", "LI"), "Limestone", data$Material)