我正在研究为旧标签分配新标签的问题。这个问题要求我
以下是我所做的但它显示的错误信息为“In if(问题$ pickup_zipcode == 94107&amp; problem $ pickup_name ==:条件长度> 1且仅使用第一个元素”< / p>
我想我不能使用if因为它不会进入下一个声明?那么“为”呢?
答案 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