嵌套在列表中的lapply()吗? [英] Nested lapply() in a list?

查看:99
本文介绍了嵌套在列表中的lapply()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表l,它具有以下功能:

I have a list l, which has the following features:

  • 它包含3个元素
  • 每个元素都是长度为5的数字矢量
  • 每个向量包含从1到5的数字
l = list(a = c(2, 3, 1, 5, 1), b = c(4, 3, 3, 5, 2), c = c(5, 1, 3, 2, 4))

我想做两件事:

第一

我想知道每个数字在整个列表中出现多少次,并且我希望每个结果都成一个向量(或任何可以让我稍后对结果进行计算的形式):

I want to know how many times each number occurs in the entire list and I want each result in a vector (or any form that can allow me to perform computations with the results later):

代码1:

> a <- table(sapply(l, "["))
> x <- as.data.frame(a)
> x

  Var1   Freq
1    1   3
2    2   3
3    3   4
4    4   2
5    5   3

无论如何都可以执行此操作而无需使用table()函数.我想手动"进行.我试着在下面做.

Is there anyway to do it without using the table() function. I would like to do it "manually". I try to do it right below.

代码2 :(我知道这不是很有效!)

Code 2: (I know this is not very efficient!)

x <- data.frame(
"1" <- sum(sapply(l, "[")) == 1
"2" <- sum(sapply(l, "[")) == 2
"3" <- sum(sapply(l, "[")) == 3
"4" <- sum(sapply(l, "[")) == 4
"5" <- sum(sapply(l, "[")) == 5)

我尝试了以下操作,但没有工作.我实际上不明白结果.

I tried the following, but I did not work. I actually did not understand the result.

> sapply(l, "[") == 1:5        

     a     b     c
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE FALSE
[3,] FALSE  TRUE  TRUE
[4,] FALSE FALSE FALSE
[5,] FALSE FALSE FALSE

> sum(sapply(l, "[") == 1:5)

[1] 2

第二

现在,我想获取每个数字出现在列表中的次数,但是现在要获取每个元素$a$b$c中的次数.我考虑过使用lapply(),但我不知道该怎么做.以下是我尝试过的方法,但是效率很低,就像 Code 2

Now, I would like to get the number of times each number appears in the list, but now in each element $a, $b and $c. I thought about using the lapply() but I don't know how exactly. Following is what I tried, but it is inefficient just like Code 2:

lapply(l, function(x) sum(x == 1))
lapply(l, function(x) sum(x == 2))
lapply(l, function(x) sum(x == 3))
lapply(l, function(x) sum(x == 4))
lapply(l, function(x) sum(x == 5))

我用这5行代码得到的是3个元素的5个列表,每个元素包含一个数字值.例如,第二行代码告诉我2l的每个元素中出现了多少次.

What I get with these 5 lines of code are 5 lists of 3 elements each containing a single numeric value. For example, the second line of code tells me how many times number 2 appears in each element of l.

代码3:

> lapply(l, function(x) sum(x == 2))

$a
[1] 1

$b
[1] 1

$c
[1] 1

我想获得的是一个包含三个元素的列表,其中包含我要查找的所有信息.

What I would like to obtain is a list with three elements containing all the information I am looking for.

请在答案中使用代码1",代码2"和代码3".非常感谢.

Please, use the references "Code 1", "Code 2" and "Code 3" in your answers. Thank you very much.

推荐答案

对于代码1/2,您可以使用sapply来获取所需值的计数:

For code 1/2, you could use sapply to obtain the counts for whichever values you wanted:

l = list(a = c(2, 3, 1, 5, 1), b = c(4, 3, 3, 5, 2), c = c(5, 1, 3, 2, 4))
data.frame(number = 1:5,
           freq = sapply(1:5, function(x) sum(unlist(l) == x)))
#   number freq
# 1      1    3
# 2      2    3
# 3      3    4
# 4      4    2
# 5      5    3

对于代码3,如果要获取列表a,b和c的计数,则可以使用lapply函数将频率函数应用于列表的每个元素:

For code 3, if you wanted to get the counts for lists a, b, and c, you could just apply your frequency function to each element of the list with the lapply function:

freqs = lapply(l, function(y) sapply(1:5, function(x) sum(unlist(y) == x)))
data.frame(number = 1:5, a=freqs$a, b=freqs$b, c=freqs$c)
#   number a b c
# 1      1 2 0 1
# 2      2 1 1 1
# 3      3 1 2 1
# 4      4 0 1 1
# 5      5 1 1 1

这篇关于嵌套在列表中的lapply()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆