Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/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:在分组do中查找子组条件_R_Data.table_Dplyr - Fatal编程技术网

dplyr:在分组do中查找子组条件

dplyr:在分组do中查找子组条件,r,data.table,dplyr,R,Data.table,Dplyr,我一直试图使用dplry进行一些相当复杂的数据处理,但遇到了这个问题,对此我无法找到答案。这是我在StackOverflow上问的第一个问题。这可能更像是一个分组的data.table问题,而不是dplyr问题,但现在开始 data(iris) df <- iris %>% group_by(Species) %>% do((function(x) { print(names(x)) print(class(x)) if(x$Species[1]

我一直试图使用dplry进行一些相当复杂的数据处理,但遇到了这个问题,对此我无法找到答案。这是我在StackOverflow上问的第一个问题。这可能更像是一个分组的data.table问题,而不是dplyr问题,但现在开始

data(iris)
df <- iris %>% group_by(Species) %>% 
  do((function(x) {
    print(names(x))
    print(class(x))
    if(x$Species[1] == 'setosa') x$Petal.Length <- x$Petal.Length+1
    return(x)
  })(.))
数据(iris)
df%组_按(物种)%>%
do((函数(x){
打印(姓名(x))
印刷品(类别(x))
如果(x$Species[1]=='setosa')x$Petal.Length%
变异(物种2=物种)%>%
组别(种类)%>%
do((函数(x){
打印(姓名(x))
印刷品(类别(x))
打印(属性(x,'vars'))
打印(组(x))
如果(x$Species2[1]=='setosa')x$Petal.Length%

子组x是一个分组的data.table,分组变量从子组中删除。我已经在do中包含了分组列的副本和该副本的引用,但我觉得应该/可能有一种更优雅的方式来引用do当前正在使用的特定组。

data.table
中分组变量作为一个原子值(而不是向量)保存,每个组的其余数据保存在一个名为
.SD
数据表中。我不太确定您想做什么,但下面是一些可能会让您继续的示例:

library(data.table)
data(iris)
setDT(iris)  # convert to data.table in place

iris[, if (Species == 'setosa') {
         Petal.Length + 1
       } else {
         Petal.Length
       }
     , by = Species]

# modify in place
iris[Species == 'setosa', Petal.Length := Petal.Length + 1]

# more complicated modification - modify the first Petal.Width by Species
iris[iris[, .I[1], by = Species]$V1, Petal.Width := Petal.Width + 4]

太好了!这让我达到了我想要的目的。不过,到目前为止,我一直只使用dplyr语法进行编码,因此,虽然很高兴看到data.tables具有我所寻找的功能,但我想我会等到有人有机会给出dplyr解决方案时再接受这个答案。@LukeHankins很酷,很乐意提供帮助。你会从中获得更多信息
data.table
如果您单独使用它,则比从
dplyr
中使用它要快,而且语法更容易理解。
library(data.table)
data(iris)
setDT(iris)  # convert to data.table in place

iris[, if (Species == 'setosa') {
         Petal.Length + 1
       } else {
         Petal.Length
       }
     , by = Species]

# modify in place
iris[Species == 'setosa', Petal.Length := Petal.Length + 1]

# more complicated modification - modify the first Petal.Width by Species
iris[iris[, .I[1], by = Species]$V1, Petal.Width := Petal.Width + 4]