Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.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
dplyr通过在另一个数据帧上应用summary函数来计算新列_R_Dplyr - Fatal编程技术网

dplyr通过在另一个数据帧上应用summary函数来计算新列

dplyr通过在另一个数据帧上应用summary函数来计算新列,r,dplyr,R,Dplyr,我想在名为df的数据帧中创建一个新列(CNT)。将使用dplyr软件包中的summary函数计算该值。它应该返回一个数字,因为我需要计算另一个数据帧(cars)中的一列,但是过滤条件由df的两列中的值决定 数据帧: library(dplyr) df <- data.frame("my_speed" = 11:20, "my_dist" = c(17,20,15,17,21,23,28,36,50,80)) 我正在试图弄清楚如何使用summary()或其他方法轻松完成这项工作。请注意,如

我想在名为
df
的数据帧中创建一个新列(
CNT
)。将使用
dplyr
软件包中的
summary
函数计算该值。它应该返回一个数字,因为我需要计算另一个数据帧(
cars
)中的一列,但是过滤条件由
df
的两列中的值决定

数据帧:

library(dplyr)
df <- data.frame("my_speed" = 11:20, "my_dist" = c(17,20,15,17,21,23,28,36,50,80))
我正在试图弄清楚如何使用
summary()
或其他方法轻松完成这项工作。请注意,如果
summary()
未返回任何记录,则应显示零

df %>% 
rowwise() %>%
filter(speed==my_spped & dist==my_dist) %>% 
summarise(count=n()) %>% 
select (count) %>% 
mutate(CNT=count)

我们可以定义一个函数

library(tidyverse)

get_count <- function(x, y) {
   cars %>% 
    summarise(count = sum(speed == x & dist == y)) %>% 
    pull(count)
}

使用
apply
时,与之相同的基本R等价物为

get_count <- function(x) {
  nrow(subset(cars, speed == x[1] & dist == x[2]))
}

df$CNT <- apply(df, 1, get_count)

get\u count使用
rowwise
,我们可以直接获得逻辑表达式的
,而无需进行额外的操作

df %>% 
   rowwise %>% 
   mutate(CNT = sum((cars$speed == my_speed) & (cars$dist == my_dist)))
# A tibble: 10 x 3
#   my_speed my_dist   CNT
#      <int>   <dbl> <int>
# 1       11      17     1
# 2       12      20     1
# 3       13      15     0
# 4       14      17     0
# 5       15      21     0
# 6       16      23     0
# 7       17      28     0
# 8       18      36     0
# 9       19      50     0
#10       20      80     0
df%>%
行%>%
变异(CNT=sum((cars$speed==my_speed)和(cars$dist==my_dist)))
#一个tibble:10x3
#我的速度我的距离
#          
# 1       11      17     1
# 2       12      20     1
# 3       13      15     0
# 4       14      17     0
# 5       15      21     0
# 6       16      23     0
# 7       17      28     0
# 8       18      36     0
# 9       19      50     0
#10       20      80     0
解决方案 资料
dat这个方法看起来很有趣,但我无法应用于我的实际问题。基本上,我在这里定义的df是一系列日期和名称。数据集
cars
在我的实际问题中是一列
name
和三列日期。我需要使用df中的名称和日期运行聚合函数。我必须对聚合函数应用许多
WHERE
子句。使用
rowwise()
的解决方案有效,但需要5分钟,我想知道这个方法会更快吗?这是使用
rowwise()
的代码,您如何将其转换为join方法
df%rowwise()%%>%mutate(NRTS=sum((df2$Entry_DateDayOf+24*60*60-1)| is.na(df2$RTS_Date))&is.na(df2$Completion_Date))
@Ibo,如果您能提供数据的代表性示例(只是一小部分),并且它可以等待明天,我将尝试解决这个问题。我以txt(空格分隔)的形式创建了两个数据帧的子集,并将它们加载到这里:我假设您不会使用能够处理我的实际问题和我提供的数据的代码来更新您的答案,因此,我将继续并标记正确的答案我已经用类似的方法解决了我的问题,但与此方法相比,使用行和和函数更快。这花费了474秒,而rowwise和sum花费了256秒。我想如果我能弄清楚正确的连接解决方案是如何工作的,它会更快性能说明:使用
Date
而不是
POSIXct
更快,对于我的真实数据,167秒比256秒更快
get_count <- function(x) {
  nrow(subset(cars, speed == x[1] & dist == x[2]))
}

df$CNT <- apply(df, 1, get_count)
df %>% 
   rowwise %>% 
   mutate(CNT = sum((cars$speed == my_speed) & (cars$dist == my_dist)))
# A tibble: 10 x 3
#   my_speed my_dist   CNT
#      <int>   <dbl> <int>
# 1       11      17     1
# 2       12      20     1
# 3       13      15     0
# 4       14      17     0
# 5       15      21     0
# 6       16      23     0
# 7       17      28     0
# 8       18      36     0
# 9       19      50     0
#10       20      80     0
library(dplyr)

cars %>%
  count(speed, dist) %>%                   # count unique (speed, dist) pairs
  right_join(dat) %>%                      # join to dat, drop all not in dat
  mutate(CNT = coalesce(n, 0L), n = NULL)  # replace NA, create CNT, drop n
dat <- data.frame(
  speed = 11:20,
  dist = c(17, 20, 15, 17, 21, 23, 28, 36, 50, 80)
  )
# A tibble: 10 x 3
   speed  dist   CNT
   <dbl> <dbl> <int>
 1    11    17     1
 2    12    20     1
 3    13    15     0
 4    14    17     0
 5    15    21     0
 6    16    23     0
 7    17    28     0
 8    18    36     0
 9    19    50     0
10    20    80     0