合并两个列表组件
我有一个大列表,但微观示例如下所示:
A <- c("A", "a", "A", "a", "A")
B <- c("A", "A", "a", "a", "a")
C <- c(1, 2, 3, 1, 4)
mylist <- list(A=A, B=B, C= C)
预期输出是将 A 与 B 合并,以便每个组件看起来像 AB
AA, aA, Aa, aa, Aa
更好应该排序,大写字母总是第一个
AA, Aa, Aa, aa, Aa
因此新列表或矩阵应该有两列或rows:
AA, Aa, Aa, aa, Aa
1, 2, 3, 1, 4
现在我想根据类“AA”、“Aa”和“aa”计算C的平均值
看起来很简单,但我无法轻易弄清楚。
I have a big list, but micro example would be like the following:
A <- c("A", "a", "A", "a", "A")
B <- c("A", "A", "a", "a", "a")
C <- c(1, 2, 3, 1, 4)
mylist <- list(A=A, B=B, C= C)
expected output is merge A with B so that each component will look like AB
AA, aA, Aa, aa, Aa
better should be sorted, upper case is always first
AA, Aa, Aa, aa, Aa
Thus new list or matrix should have two columns or rows:
AA, Aa, Aa, aa, Aa
1, 2, 3, 1, 4
Now I want calculate average of C based on class - "AA", "Aa", and "aa"
Looks simple but I could not figure out easily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的数据使用
plyr
包排列在data.frame
中,我就可以做到这一点I can do it if your data is arranged in a
data.frame
using the packageplyr
使用您的数据:
我使用 A 和 B 的组合作为关键列定义一个
data.frame
:如果您需要在聚合之前订购此
data.frame
则:使用
aggregate
,您可以计算每个id
的平均值:但是如果您想在聚合后进行排序,那么:
With your data:
I define a
data.frame
using the combination of A and B as the key column:If you need to order this
data.frame
before the aggregation then:And with
aggregate
you calculate the mean for eachid
:But if you want to order after the aggregation, then: