【问题标题】:Subsetting by two factors, all levels, with a simple code用一个简单的代码按两个因素,所有级别的子集
【发布时间】:2014-12-25 23:33:24
【问题描述】:

我知道这个问题很简单,但是如果不创建步骤对象就找不到解决方案,我想要一个单行代码,或者尽可能简单的代码。

假设我有一个名为 df 的数据框,其中包含 x、y、z 列:

x<-c(rep('place1',33),rep('place2',33),rep('place3',34))
y<-sample(c('type1','type2','type3','type4','type5'),100,replace=T)
z<-sample(40:80,100,replace=T)
df<-data.frame(x,y,z)

我想为 x 和 y 的每个级别组合获取所有可能的 z 子集(type1 in place1,type2 in place1, type3 in place1...type4 in place3 和 type5 in place3)。像这样的:

[[place1]]
[type1]
[1] 57 73 74 47 52 61

[type2]
[1] 72 76 64 62 73 75
...

[type5]
...

[[place3]]
[type1]
...

[type5]

如果这是可能的,我如何访问每个子集?

我尝试在lapply 中嵌套split,但没有成功。

很抱歉这个简单的问题,但找不到合适的解决方案。

任何帮助将不胜感激。

【问题讨论】:

    标签: r subset lapply


    【解决方案1】:

    这是一种方法。您使用变量 x 拆分 df。然后,使用变量 y 再次使用 split 拆分每个数据帧。通过这种方式,您可以以您想要的方式对数据进行子集化。我最后留下了一些修剪的结果。

    lapply(split(df, f = df$x), function(x) split(x, f = x$y)
    
    #$place1
    #$place1$type1
    #        x     y  z
    #5  place1 type1 46
    #7  place1 type1 41
    
    #$place1$type2
    #        x     y  z
    #3  place1 type2 44
    #4  place1 type2 59
    

    如果你只想要 z 的值,你可以这样做:

    lapply(split(df, f = df$x), function(x) split(x$z, f = x$y))
    
    #$place1
    #$place1$type1
    #[1] 46 41 50 59 54 51 66 70
    
    #$place1$type2
    #[1] 44 59 60 53 74 46 67 70
    
    #$place1$type3
    #[1] 63 70 80 44 73 74 58
    
    #$place1$type4
    #[1] 45 67 52 72 45 48 79 65
    
    #$place1$type5
    #[1] 75 54
    

    编辑

    看到@user295691提供的链接,你也可以这样做。

    split(df$z, interaction(df$x,df$y))
    

    如果你希望每个向量都有 z 值,你可以这样做:

    list2env(split(df$z, interaction(df$x,df$y)), .GlobalEnv)
    

    EDIT2

    OP 想要使用这些数据运行统计信息。因此,我认为留下以下内容是个好主意。如果您需要在列表中创建具有不同向量长度的数据框,您可以执行以下操作。 listvectors2df 让你创建一个 NA 的数据框。

    ana <- split(df$z, interaction(df$x,df$y))
    
    # I used a good answer in this post and wrote the following.
    #http://stackoverflow.com/questions/15201305/how-to-convert-a-list-consisting-of-vector-of-different-lengths-to-a-usable-data
    
    listvectors2df <- function(l){
    
        n.obs <- sapply(l, length)
        seq.max <- seq_len(max(n.obs))
        mydf <- data.frame(sapply(l, "[", i = seq.max), stringsAsFactors = FALSE)
    
    }
    
    bob <- listvectors2df(ana)
    

    【讨论】:

    • 非常感谢您的回答。这正是我想要的。而且,我想访问 z 列并将其作为向量放在每个列表元素中,而不是 df 中的所有列。这可能吗?
    • @JoseRamon 非常感谢您的评论。我想知道lapply(split(df, f = df$x), function(x) split(x$z, f = x$y)) 是否符合您的要求。如果您想要每个向量,您可以执行list2env(split(df$z, interaction(df$x,df$y)), .GlobalEnv) 之类的操作。然后,输入ls()。您将看到所有向量。如果这是您想要的,请告诉我。
    • 我刚刚测试了list2env 解决方案。不知道可以为每个列表元素创建一个向量。这对我来说真的很有用,不仅对于这个特定的问题,而且对于我遇到的许多其他问题。让我再问你一个问题,现在我们已经到了这一步:我可以在所有可能的配对之间运行,例如wilcox.test(当然不是配对)吗?例如:place1.type1 与 place1.type2...place5.type4 与 place5.type5。谢谢
    • @JoseRamon 让我想想我可以尝试什么。明天我会回复你的。
    • @JoseRamon 我不认为我可以在这里写所有东西。但是,您首先要创建一个数据框。检查this link。您可以学习如何创建一个 df。我使用dplyr 运行了 wilcox.test。您不能运行所有对。但是您可以用一列对其余列运行测试。如果 mydf 是您的 df,您可以执行以下操作:mydf %>% summarise_each(funs(wilcox.test(place1.type1,.)$p.value), vars=place2.type1:place3.type5)。由于关系,你有警告。希望这对您有所帮助。
    【解决方案2】:

    也可以在交互中使用拆分:

    split(df, interaction(x,y))
    $place1.type1
            x     y  z
    6  place1 type1 57
    25 place1 type1 55
    27 place1 type1 55
    28 place1 type1 75
    29 place1 type1 54
    
    $place2.type1
            x     y  z
    36 place2 type1 70
    42 place2 type1 69
    45 place2 type1 78
    57 place2 type1 79
    59 place2 type1 46
    60 place2 type1 45
    63 place2 type1 73
    64 place2 type1 79
    
    $place3.type1
            x     y  z
    85 place3 type1 54
    

    访问每个元素:

    > ll = split(df, interaction(x,y))
    > 
    > ll[[1]]
            x     y  z
    6  place1 type1 57
    25 place1 type1 55
    27 place1 type1 55
    28 place1 type1 75
    29 place1 type1 54
    > 
    > ll[[2]]
            x     y  z
    36 place2 type1 70
    42 place2 type1 69
    45 place2 type1 78
    57 place2 type1 79
    59 place2 type1 46
    60 place2 type1 45
    63 place2 type1 73
    64 place2 type1 79
    

    也可以使用data.table:

    library(data.table)
    dtt = data.table(df)
    
    dtt[order(x,y),list(meanz=mean(z), maxz=max(z), sumz=sum(z)),by=list(x,y)]
             x     y    meanz maxz sumz
     1: place1 type1 63.11111   80  568
     2: place1 type2 68.12500   79  545
     3: place1 type3 58.80000   76  294
     4: place1 type4 59.83333   79  359
     5: place1 type5 59.40000   80  297
     6: place2 type1 55.85714   69  391
     7: place2 type2 59.71429   71  418
     8: place2 type3 61.00000   76  305
     9: place2 type4 53.63636   71  590
    10: place2 type5 44.66667   46  134
    11: place3 type1 62.16667   74  373
    12: place3 type2 63.42857   80  444
    13: place3 type3 64.00000   77  384
    14: place3 type4 61.28571   80  429
    15: place3 type5 51.00000   60  408
    

    【讨论】:

    • 包data.table 和split 的interaction 参数中的解决方案非常有趣。谢谢。
    【解决方案3】:

    有几个解决方案。第一个是 jazzurro 提供的 lapply/split。您还可以将这些因素组合成一个因素,例如

    > split(df, paste(df$x, df$y))
    $`place1 type1`
            x     y  z
    3  place1 type1 57
    24 place1 type1 54
    
    $`place1 type2`
            x     y  z
    1  place1 type2 67
    6  place1 type2 75
    7  place1 type2 72
    12 place1 type2 57
    ...
    

    另一种解决方案是使用对多级分组具有内在支持的库,例如 data.tables 或 plyr/dplyr。在dplyr 中,操作看起来像(包括摘要,在这种情况下是第三列的平均值和最大值)

    > df %>% group_by(x, y) %>% summarise(mean(z), max(z))
    Source: local data frame [15 x 4]
    Groups: x
    
            x     y  mean(z) max(z)
    1  place1 type1 55.50000     57
    2  place1 type2 65.50000     80
    3  place1 type3 60.40000     78
    4  place1 type4 57.12500     73
    ...
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多