代码之家  ›  专栏  ›  技术社区  ›  Banjo

替换包含特定字符串模式的变量值

  •  1
  • Banjo  · 技术社区  · 7 年前

    library(dplyr)
    glimpse(full_dat)
    Observations: 9,720
    Variables: 6
    $ Product <chr> "Apple iPhone 4s 8GB Unlocked GSM Smartphone w/ S...
    $ Brand   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
    $ Price   <dbl> 115, 115, 115, 115, 115, 115, 115, 115, 115, 115,...
    $ Rating  <dbl> 5, 1, 4, 5, 5, 3, 5, 5, 5, 1, 5, 5, 1, 5, 2, 5, 5...
    $ Reviews <chr> "It was new and at a great price! Phone came real...
    $ Votes   <dbl> 2, 1, 0, 1, 2, 2, 2, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0...
    

    我想更改变量的值 Product

    伪代码:

    glimpse(full_dat)
    Observations: 9,720
    Variables: 6
    $ Product <chr> "iPhone 4s", "iPhone 4s", "iPhone 4s", "iphone 4s...
    $ Brand   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
    $ Price   <dbl> 115, 115, 115, 115, 115, 115, 115, 115, 115, 115,...
    $ Rating  <dbl> 5, 1, 4, 5, 5, 3, 5, 5, 5, 1, 5, 5, 1, 5, 2, 5, 5...
    $ Reviews <chr> "It was new and at a great price! Phone came real...
    $ Votes   <dbl> 2, 1, 0, 1, 2, 2, 2, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0...
    

    我读了一篇类似的文章,其中提出了以下解决方案。

    full_dat %>% 
      mutate_at(vars(contains('iphone 4s')), funs(.=='ipohne 4s'))
    

    但是,这在我的情况下不起作用,即值保持不变。

    product = c(full_dat$Product[1:5])
    dput(product)
    
    c("Apple iPhone 4s 8GB Unlocked GSM Smartphone w/ Siri, iCloud and 8MP Camera - Black", 
    "Apple iPhone 4s 8GB Unlocked GSM Smartphone w/ Siri, iCloud and 8MP Camera - Black", 
    "Apple iPhone 4s 8GB Unlocked GSM Smartphone w/ Siri, iCloud and 8MP Camera - Black", 
    "Apple iPhone 4s 8GB Unlocked GSM Smartphone w/ Siri, iCloud and 8MP Camera - Black", 
    "Apple iPhone 4s 8GB Unlocked GSM Smartphone w/ Siri, iCloud and 8MP Camera - Black"
    )
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Ronak Shah    7 年前

    我想你在找我

    library(dplyr)
    
    samp %>%
       mutate_at(vars(Product), funs(replace(., grepl('iPhone 4s', .), 'iphone 4s')))
    

    这将 replace Product 它包含“iPhone4S”到“iPhone4S”。

    当然,你可以不用担心 dplyr

    df$Product <- with(samp, replace(Product, grepl('iPhone 4s', Product),'iPhone 4s'))
    
    推荐文章