Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vlookup()等价于Dplyr_R_Join_Dplyr_Tidyverse - Fatal编程技术网

Vlookup()等价于Dplyr

Vlookup()等价于Dplyr,r,join,dplyr,tidyverse,R,Join,Dplyr,Tidyverse,我的df看起来像: library(tidyverse) df_1 <- tibble::tribble( ~sub_date, ~value_1, ~value_2, "2020-05", 58, 130, "2020-05", 58, "check", "2020-03", 50, 120, "2020-03",

我的df看起来像:

library(tidyverse) 
df_1 <- tibble::tribble(
          ~sub_date, ~value_1, ~value_2,
          "2020-05",       58,      130,
          "2020-05",       58,       "check",
          "2020-03",       50,      120,
          "2020-03",       55,       "check",
          "2020-03",       55,       "check"
          )
这个代码应该有效

df_1 %>% 
  mutate(value_2 = as.numeric(na_if(value_2, "check"))) %>% 
  left_join(df_ref, by = "sub_date") %>% 
  mutate(value_2 = coalesce(value_2, ref_value)) %>% 
  select(-ref_value)
小说明:首先我们将所有
值设置为
NA
“check”
值,这要归功于
NA\u if
,然后我们加入查找表,然后我们
合并
两列
value\u 2
ref\u value
,即在这两列之间取第一个不缺失的值


输出

# A tibble: 5 x 3
#   sub_date value_1 value_2
#   <chr>      <dbl>   <dbl>
# 1 2020-05       58     130
# 2 2020-05       58     123
# 3 2020-03       50     120
# 4 2020-03       55     234
# 5 2020-03       55     234

如果有多场比赛,祝你好运

library(tidyverse)
df_1 <- tibble::tribble(
  ~sub_date, ~value_1, ~value_2,
  "2020-05",       58,      "130",
  "2020-05",       58,       "check",
  "2020-03",       50,      "120",
  "2020-03",       55,       "check",
  "2020-03",       55,       "check"
)

df_ref <- tibble::tribble(
  ~sub_date, ~ref_value,
  "2020-05",        123,
  "2020-03",        234
)

df_1 %>% 
  left_join(df_ref) %>%
  mutate(value_2_true = ifelse(value_2 == "check",ref_value,value_2)) %>%
  mutate(value_2 = value_2_true %>% as.numeric()) %>% 
  select(-value_2_true,-ref_value)
库(tidyverse)
df_1%
变异(值_2_true=ifelse(值_2==“检查”,参考值,值_2))%>%
变异(value_2=value_2_true%>%as.numeric())%%>%
选择(-value\u 2\u true,-ref\u value)
使用基本R:

ref_lut <- with(df_ref, setNames(ref_value, sub_date))

df_1$value_2 <- 
  ifelse(df_1$value_2 == "check", ref_lut[df_1$sub_date], df_1$value_2)

df_1

  sub_date value_1 value_2
  <chr>      <dbl> <chr>  
1 2020-05       58 130    
2 2020-05       58 123    
3 2020-03       50 120    
4 2020-03       55 234    
5 2020-03       55 234    

ref\u lut您也可以使用
ifelse
match
执行一行操作,因为我们对加入数据帧并不感兴趣。如果需要数值输出,请将其包装为.numeric

库(dplyr)
突变(df_1,
值_2=ifelse(值_2==“检查”,
df_ref$ref_值[匹配(子_日期,df_ref$sub_日期)],
价值(2))

看起来您正在尝试加入。我理解它是join,但只针对行的子集,而不是整个dataframe@cory。我不明白当它应用于具有特定字符串的行时该怎么做是的,vlookup实际上是连接的一个非常糟糕的实现,如果你想看到完全错误的结果,你需要以某种方式对它们进行过滤,只是顶部的结果。对了,你明白了。把它分成两部分。。。连接以创建一个新列,然后使用ifelse将其分到您的列中。
df_1 <- tibble::tribble(
  ~sub_date, ~value_1, ~value_2,
  "2020-05",       58,      "130",
  "2020-05",       58,      "check",
  "2020-03",       50,      "120",
  "2020-03",       55,      "check",
  "2020-03",       55,      "check"
)
library(tidyverse)
df_1 <- tibble::tribble(
  ~sub_date, ~value_1, ~value_2,
  "2020-05",       58,      "130",
  "2020-05",       58,       "check",
  "2020-03",       50,      "120",
  "2020-03",       55,       "check",
  "2020-03",       55,       "check"
)

df_ref <- tibble::tribble(
  ~sub_date, ~ref_value,
  "2020-05",        123,
  "2020-03",        234
)

df_1 %>% 
  left_join(df_ref) %>%
  mutate(value_2_true = ifelse(value_2 == "check",ref_value,value_2)) %>%
  mutate(value_2 = value_2_true %>% as.numeric()) %>% 
  select(-value_2_true,-ref_value)
ref_lut <- with(df_ref, setNames(ref_value, sub_date))

df_1$value_2 <- 
  ifelse(df_1$value_2 == "check", ref_lut[df_1$sub_date], df_1$value_2)

df_1

  sub_date value_1 value_2
  <chr>      <dbl> <chr>  
1 2020-05       58 130    
2 2020-05       58 123    
3 2020-03       50 120    
4 2020-03       55 234    
5 2020-03       55 234