Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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
R 数据帧之间的查找。如果存在匹配项,则返回计数、最大值和最小值_R_Lookup - Fatal编程技术网

R 数据帧之间的查找。如果存在匹配项,则返回计数、最大值和最小值

R 数据帧之间的查找。如果存在匹配项,则返回计数、最大值和最小值,r,lookup,R,Lookup,我是R的新手,似乎我找到的解决问题的方法花费了太多的时间 我有两个数据帧: UniqueID colA colB 1 x y 2 x y 3 x y 4 x y 及 我想获得一个数据帧,稍后将附加到第一个数据帧,类似于(假设d1

我是R的新手,似乎我找到的解决问题的方法花费了太多的时间

我有两个数据帧:

 UniqueID  colA colB
 1          x     y
 2          x     y
 3          x     y
 4          x     y

我想获得一个数据帧,稍后将附加到第一个数据帧,类似于(假设d1 对于每个Id,我需要计算它在第二个数据帧中出现的总次数,然后计算每个类别的出现次数,并记录早期和最新的观察结果

我所能做的就是使用for循环:


iteractions以下是一种
tidyverse
方法:

library(tidyverse);
printDate <- function(x) format(x, "%d-%m-%Y");
left_join(
    df2 %>% mutate(date = as.Date(date, format = "%d-%m-%Y")),
    df1) %>%
    group_by(UniqueID) %>%
    summarise(
        totaloccurrences = n(),
        occurrencescatA = sum(category == "a"),
        MindatecatA = ifelse(occurrencescatA > 0, printDate(min(date[category == "a"])), "0"),
        MaxdatecatA = ifelse(occurrencescatA > 0, printDate(max(date[category == "a"])), "0"))
## A tibble: 3 x 5
#  UniqueID totaloccurrences occurrencescatA MindatecatA MaxdatecatA
#     <int>            <int>           <int> <chr>       <chr>
#1        1                3               2 01-05-2018  02-05-2018
#2        2                1               0 0           0
#3        3                2               2 05-05-2018  06-05-2018

谢谢你的回答!我正在尝试这种方法,但我遇到了一些问题-我怀疑是因为UniqueID列是一个字符串-返回以下消息:Error in UseMethod(“tbl_vars”):没有适用于“character”@rebtevye类对象的“tbl_vars”方法。请确认我的示例代码与示例数据一起工作。如果
UniqueID
字符
向量,代码也可以工作;所以我不确定错误的来源。我建议从我的示例开始,试着理解每一步都做了什么,然后将代码应用到实际数据中。是的,我明白了。代码实际上与示例一起工作,我需要找出它。我成功地修复了它,感谢您的帮助。我将df1简化为一个更简单的实体,保留了所需的列。不过,我将逐步返回到起始场景,因为我想了解它为什么不起作用
 UniqueID    totaloccurrences  occurrencescatA MindatecatA MaxdatecatA
 1                  3                 2             d1          d2
 2                  1                 0             0            0
 3                  2                 2             d5           d6
library(tidyverse);
printDate <- function(x) format(x, "%d-%m-%Y");
left_join(
    df2 %>% mutate(date = as.Date(date, format = "%d-%m-%Y")),
    df1) %>%
    group_by(UniqueID) %>%
    summarise(
        totaloccurrences = n(),
        occurrencescatA = sum(category == "a"),
        MindatecatA = ifelse(occurrencescatA > 0, printDate(min(date[category == "a"])), "0"),
        MaxdatecatA = ifelse(occurrencescatA > 0, printDate(max(date[category == "a"])), "0"))
## A tibble: 3 x 5
#  UniqueID totaloccurrences occurrencescatA MindatecatA MaxdatecatA
#     <int>            <int>           <int> <chr>       <chr>
#1        1                3               2 01-05-2018  02-05-2018
#2        2                1               0 0           0
#3        3                2               2 05-05-2018  06-05-2018
df1 <- read.table(text =
    "UniqueID  colA colB
 1          x     y
 2          x     y
 3          x     y
 4          x     y", header = T)


df2 <- read.table(text =
    "UniqueID   category   date
 1           a        01-05-2018
 1           a        02-05-2018
 1           b        03-05-2018
 2           c        04-05-2018
 3           a        05-05-2018
 3           a        06-05-2018", header = T)