【问题标题】:pairwise substring a data frame and save them in a list成对子串一个数据框并将它们保存在一个列表中
【发布时间】:2021-03-28 13:15:16
【问题描述】:
# Example data
dat <- matrix(runif(2*300), ncol = 2, nrow = 20)
group <- rep_len(LETTERS[1:3], 20)
df <- cbind.data.frame(dat, Group = group)

# Greate subset groups
n <- levels(as.factor(group))
mylist <- combn(n, 2, simplify = FALSE)

我想根据group属性的成对组合对我的数据进行子集化,然后将结果保存在mylist中。

我该怎么做?

非常感谢。

【问题讨论】:

    标签: r list dataframe subset


    【解决方案1】:

    我们可以在循环mylist之后使用subset%in%

    mylist2 <- lapply(mylist, function(x) subset(df, Group %in% x))
    

    也可以通过FUN 参数在combn 内完成

    combn(n, 2, FUN = function(x) subset(df, Group %in% x), simplify = FALSE)
    

    【讨论】:

    • 太棒了!非常感谢,@akrun。
    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多