Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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中使用逻辑运算符_R_Dplyr_Logical Operators - Fatal编程技术网

在变异dplyr中使用逻辑运算符

在变异dplyr中使用逻辑运算符,r,dplyr,logical-operators,R,Dplyr,Logical Operators,我有一个如下所示的数据框: df = data.frame(animals = c("cat; dog; bird", "dog; bird", "bird"), sentences = c("the cat is brown; the dog is barking; the bird is green and blue", "the bird is yellow and blue", "the bird is blue"),year= c("2010","2012","2001"), stri

我有一个如下所示的数据框:

df = data.frame(animals = c("cat; dog; bird", "dog; bird", "bird"), sentences = c("the cat is brown; the dog is barking; the bird is green and blue", "the bird is yellow and blue", "the bird is blue"),year= c("2010","2012","2001"), stringsAsFactors = F)

df$year <-  as.numeric(df$year)

> df
         animals                                                        sentences year
1       cat; dog                bird the cat is brown; the bird is green and blue 2010
2      dog; bird                    the dog is black; the bird is yellow and blue 2012
3           bird                                                 the bird is blue 2001
df=data.frame(动物=c(“猫;狗;鸟”,“狗;鸟”,“鸟”),句子=c(“猫是棕色的;狗在叫;鸟是绿色和蓝色的”,“鸟是黄色和蓝色的”,“鸟是蓝色的”),年份=c(“2010”,“2012”,“2001”),stringsAsFactors=F)
df$年df
动物年
1只猫;狗鸟猫是棕色的;这只鸟是绿色和蓝色的
2只狗;鸟狗是黑色的;这只鸟是黄色和蓝色的
这只鸟是蓝色的
我想得到过去5年(包括同一年)中列句子中的动物总数

编辑

例如:在第2行中,动物狗和鸟在过去5年(包括同一年)=2012年,句子列中重复3次:狗为黑色;为黄色和蓝色,2010年:鸟为绿色和蓝色,总计为3

期望的结果

# A tibble: 3 x 4
  animals        sentences                                                         year   SUM
  <chr>          <chr>                                                            <dbl> <int>
1 cat; dog; bird the cat is brown; the bird is green and blue                      2010     2
2 dog; bird      the dog is black; the bird is yellow and blue                     2012     3
3 bird           the bird is blue                                                  2001     1
#一个tible:3 x 4
动物年总数
1只猫;狗;鸟猫是棕色的;这只鸟是绿色和蓝色的
2只狗;鸟狗是黑色的;这只鸟是黄色和蓝色的
这只鸟是蓝色的
解决方案

我使用了来自的以下代码并添加了逻辑运算符:
动物[(年份>=第5年)和(年份%)
突变(总和=str_分裂(动物[(年份>=第5年)和(年份%)
映射(~str_计数(字符串,)%%>%
未列出%>%sum)
任何帮助都将不胜感激:)。

尝试:

library(dplyr)

df %>% 
  mutate(SUM = sapply(strsplit(animals, "; "), length),
         SUM = sapply(year, function(x) sum(SUM[between(year, x - 5 + 1, x)])))
这是输出:

         animals                                                        sentences year SUM
1 cat; dog; bird the cat is brown; the dog is barking; the bird is green and blue 2010   3
2      dog; bird                    the dog is black; the bird is yellow and blue 2018   2
3           bird                                                 the bird is blue 2001   1
当然,在
2010
中,它与您想要的输出不一致,因为您以前没有提供数据。

尝试:

library(dplyr)

df %>% 
  mutate(SUM = sapply(strsplit(animals, "; "), length),
         SUM = sapply(year, function(x) sum(SUM[between(year, x - 5 + 1, x)])))
这是输出:

         animals                                                        sentences year SUM
1 cat; dog; bird the cat is brown; the dog is barking; the bird is green and blue 2010   3
2      dog; bird                    the dog is black; the bird is yellow and blue 2018   2
3           bird                                                 the bird is blue 2001   1

当然,在
2010
中,它不符合您的期望输出,因为您以前没有提供数据。

为什么在
中使用+1(年份,x-5+1,x)
?因为如果你想要一个5年的期限,比如说你想要2015年,这个期限从2015-5+1=2011开始,而不是从2015-5=2010开始。当然你可以说
-4
,但这是一种保持透明的方法窗口是什么?所以你总是知道要操纵哪个数字(例如,在3年期间,您只需将5替换为3,等等)谢谢你,这里只有最后一个问题,我必须用
string在句子栏中查找动物的名字,不确定想要的输出是什么,最好开始另一个主题或修改这个主题,因为我没有看到直接链接。嗯,我试着理解,但不确定。为了清楚起见,我认为最好你把这个放在我的n另一个问题,或者完全重构当前的问题。例如,我看不出
狗在哪里出现两次,等等。你需要有正确的输入和清晰的期望输出-当
字符串
未列出时会发生什么,你期望什么样的输出?等等。为什么在
中使用+1(年份,x-5+1,x)
?因为如果你想要一个5年的期限,比如说你想要2015年,这个期限从2015-5+1=2011开始,而不是从2015-5=2010开始。当然你可以说
-4
,但这是一种保持透明的方法窗口是什么?所以你总是知道要操纵哪个数字(例如,在3年期间,您只需将5替换为3,等等)谢谢你,这里只有最后一个问题,我必须用
string在句子栏中查找动物的名字,不确定想要的输出是什么,最好开始另一个主题或修改这个主题,因为我没有看到直接链接。嗯,我试着理解,但不确定。为了清楚起见,我认为最好你把这个放在我的n另一个问题或完全重构当前问题。例如,我看不出
狗出现两次的位置,等等。您需要有正确的输入和清晰的所需输出-未列出时
字符串会发生什么情况,您期望什么样的输出?等等。。