申请并申请列表R的每个组成部分和元素 [英] lapply and apply for each component and element of a list R

查看:87
本文介绍了申请并申请列表R的每个组成部分和元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此列表:

lst

lst <- list(a=c(2.5,9.8,5.0,6.7,6.5,5.2,34.4, 4.2,39.5, 1.3,0.0,0.0,4.1,0.0,0.0,25.5,196.5, 0.0,104.2,0.0,0.0,0.0,0.0,0.0),b=c(147.4,122.9,110.2,142.3))

我想为列表的每个值和列表的每个元素(ab)计算一个z.score:(x[i]-mean(x)/sd(x),其中x是每个值的所有值(togheter)列表元素和x [i]每个列表元素的每个单个组成部分. 我尝试了lapply

I would like to calculate for each values of a list and for each element of a list (a and b) a z.score as: (x[i]-mean(x)/sd(x), where x are all values (togheter) of each element of a list and x[i] each single component of each list element. I tried with lapply

lapply(lst,function (x) as.data.frame(apply(x,2, function(y)- lapply(lst,mean)/lapply(lst,sd))))

但是有一个错误... 也许使用for循环为:

but there is an error... maybe with for loop as:

lst.new <- vector("list",1)

for (i in 1:length(lst)){
  for (j in 1:dim(data.frame(lst[i]))[1]){
    res[j] <- (as.numeric(unlist(lst[i]))[j]-mean(as.numeric(unlist(lst[i])))/
      sd(as.numeric(unlist(lst[i])))
    lst.new[[i]] <- res
  }
}

但是结果很奇怪(确保在lst.new输出中我错了):

but the result is strange (sure I'm wrong in the lst.new output):

[[1]]
 [1] -0.3635464 -0.1982809 -0.3069486 -0.2684621 -0.2729899 -0.3024208  0.3586413 -0.3250599  0.4741007 -0.3907133
[11] -0.4201442 -0.4201442 -0.3273238 -0.4201442 -0.4201442  0.1571532  4.0284412 -0.4201442  1.9388512 -0.4201442
[21] -0.4201442 -0.4201442 -0.4201442 -0.4201442

[[2]]
 [1]  0.9671130 -0.4517055 -1.1871746  0.6717671 -0.2729899 -0.3024208  0.3586413 -0.3250599  0.4741007 -0.3907133
[11] -0.4201442 -0.4201442 -0.3273238 -0.4201442 -0.4201442  0.1571532  4.0284412 -0.4201442  1.9388512 -0.4201442
[21] -0.4201442 -0.4201442 -0.4201442 -0.4201442

预期结果可以是长度不同的列表或数据框,例如:

the expected result can be a list or a data frame with different length as:

 a       b
   -0.36    0.967113
  -0.19     -0.45
    [...]  [...]

以此类推...

P.S: 
 0.36 == (2.5- mean(unlist(lst[1])))/sd(unlist(lst[1]))
 0.967113 == (147.4 -mean(unlist(lst[2])))/sd(unlist(lst[2]))

对我来说,最好使用lapply(或他的家庭功能)并解决问题

It's better for me to use lapply (or his family function) and to resolve the problem

推荐答案

仅出于完整性考虑,如果没有@akrun指出的scale函数,您的代码应该是:

Just for completeness' sake, if there wasn't the scale function @akrun pointed out, your code should have been:

lapply(lst,function(x) x-mean(x)/sd(x)) 

apply内的所有lapply表示您正在尝试计算单个值的meansd ...

all those lapplys within applys mean you're trying to calculate the mean and sd of individual values...

让我们逐步解决它. lapply接受lst并将其分解为元素.每个元素依次作为匿名函数的参数给出.这意味着该函数将获得数字向量.然后,使用R的向量化,我们为向量的每个元素计算该元素的结果,减去整个向量的mean除以整个向量的sd.

Let's work through it step by step. lapply takes lst and breaks it down into elements. Each element in turn is given as the argument to your anonymous function. That means the function gets a vector of numbers. Then, using R's vectorization, what we do is calculate for every element of the vector the result of that element, minus the mean of the whole vector divided by the sd of the whole vector.

将其与代码中发生的事情进行比较:

Compare that with what happens in your code:

lapply(lst,function (x) as.data.frame(apply(x,2, function(y)- lapply(lst,mean)/lapply(lst,sd))))

因此,第一个lapply中断lst并将向量一次发送给您的函数.

So the first lapply breaks lst and sends the vectors one at a time to your function.

然后,该函数必须将向量按列分解(具有维参数2的apply)-在这里抛出错误.但是,即使它成功地将向量分解为元素,您也可以再使用两个lapply分解单个元素,并分别为它们计算meansd.

The function then has to break the vector down by columns (apply with dimension argument 2) - which is where it throws the error. But even if it succeeded to just break down the vector into elements, you then have two more lapplys that break down that single element and calculate the mean and sd for them individually.

这篇关于申请并申请列表R的每个组成部分和元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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