R在满足两个条件时为旧数据分配新标签

时间:2017-11-22 19:49:38

标签: r

我正在研究为旧标签分配新标签的问题。这个问题要求我

  1. 在每个Whole Foods Market添加标签'-H1',邮编为94107
  2. 在每个Safeway上添加标签'-H2',邮政编码为94107
  3. 在每个Pizzeria Delfina处添加标签'-H3',邮政编码为94110
  4. 以下是我所做的但它显示的错误信息为“In if(问题$ pickup_zipcode == 94107&amp; problem $ pickup_name ==:条件长度> 1且仅使用第一个元素”< / p>

    enter image description here

    我想我不能使用if因为它不会进入下一个声明?那么“为”呢?

2 个答案:

答案 0 :(得分:0)

到目前为止发布的other answer声称是data.table方法,但替换了每个硬编码的项目。因此,我觉得有必要发布一个替代解决方案,该解决方案使用查找表更新加入

library(data.table)

# read data from google drive
DT <- fread("https://drive.google.com/uc?id=1DEdJvAdACVv_Pc5IcgFBSGvDKm_GPrNE&export=download")

# create lookup table
lookup <- data.table(pickup_name = c("Safeway", "Whole Foods Market", "Pizzeria Delfina"),
                     pickup_zipcode = c(94107, 94107, 94110),
                     label = c("-H2", "-H1", "-H3")
)

# join with lookup table and update on join
DT[lookup, on = .(pickup_name, pickup_zipcode), pickup_name := paste0(pickup_name, label)]

# verify data are updated
DT[pickup_name %like% "-H.$", .(pickup_name, pickup_zipcode)]
                pickup_name pickup_zipcode
   1:            Safeway-H2          94107
   2: Whole Foods Market-H1          94107
   3:            Safeway-H2          94107
   4: Whole Foods Market-H1          94107
   5: Whole Foods Market-H1          94107
  ---                                     
2003:            Safeway-H2          94107
2004: Whole Foods Market-H1          94107
2005:            Safeway-H2          94107
2006:   Pizzeria Delfina-H3          94110
2007: Whole Foods Market-H1          94107

fread使用this hint直接从Google云端硬盘读取数据。 DT有大约60 K行和22列(磁盘上大约9 MB)。

答案 1 :(得分:-1)

这是一种Layer A方法。您可能必须安装data.table库:

data.table