在R中对不同的数据。帧应用一个函数

时间:2023-01-12 12:46:50

I´m a newbie R and I would like to ask the following question.

我一个新手´R和我想问下面的问题。

I have a group of data.frames with 2 columns, and I´d like to apply a function to eac data.frame without typing the name of the data.frame each time, i.e:

我有一群data.frames 2列,我´想应用一个函数来eac data.frame没有打字data.frame每次的名称,即:

The data.frames would be: a, b, c, d, ......... and each data.frame has 2 columns with different row length:

这些数据是:a, b, c, d, and…每一帧有两列不同的行长:

 a[1:4]
V1         V2
1   877.0578   609.0308
2   989.5682   160.1206
3  1049.5844   143.2906
4  1111.5798   214.5290

I would like to apply the following function to each data.frame, This is the function which I would like to apply

我想把下面的函数应用到每个data.frame,这就是我想要应用的函数

  as.MassPeaks <- function(x) {return(createMassPeaks(mass=mass(x), intensity=intensity(x),metaData=metaData(x)))}

I have a vector (names) with all the data.frame names I´ve tried the following code but it didn´t work

我有一个向量(名称)和所有data.frame名称我´已经试过下面的代码,但没有´t工作

 for(i in length(names)){
    assign(i, createMassPeaks(mass=i[, 1], intensity=i[, 2]))
    }

However when I try with a single data.frame it works

然而,当我尝试使用单个数据时,它可以工作

 p <- createMassPeask(mass=a[,1], intensity=[, 2])

2 个解决方案

#1


2  

You want to put the data frames into a list, and then use lapply to create a list out of that:

您想要将数据帧放入一个列表,然后使用lapply创建一个列表:

data.frames = list(a, b, c, d)
mass.peaks.each = lapply(data.frames, as.MassPeaks)

Now mass.peaks.each is a list of of the results from each.

现在mass.peaks。每个都是每个结果的列表。

Incidentally, I think you want to change as.MassPeaks to:

顺便说一句,我想你想把a换成a。MassPeaks:

  as.MassPeaks <- function(x) {return(createMassPeaks(mass=mass(x[, 1]), intensity=intensity(x[, 2]),metaData=metaData(x)))}

#2


2  

Your for fails because you forget to use get function

你的for失败是因为你忘记使用get函数

library(MALDIquant)  # You should write what package is used

`Names <- c('a', 'b','c')` # this a vector of names

for(i in Names){
  assign(i, createMassPeaks(mass=get(i)[, 1], intensity=get(i)[, 2]))
}

This loop should work. Let's test it

这个循环。让我们做个测试

set.seed(001)
a <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
b <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
c <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))

The result looks like this:

结果是这样的:

> a
S4 class type            : MassPeaks           
Number of m/z values     : 50                  
Range of m/z values      : 889.265 - 1079.764  
Range of intensity values: 9.098e+02 - 1.12e+03
> b
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 904.282 - 1104.358   
Range of intensity values: 9.256e+02 - 1.115e+03
> c
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 855.554 - 1124.883   
Range of intensity values: 8.798e+02 - 1.132e+03

In spite of this for loop works well, @David Robinson's answer is the best one. I want to say one more thing, I think your function should look like this:

尽管for loop运行良好,但@David Robinson的答案是最好的。我想说的是,你的函数应该是这样的:

as.MassPeaks <- function(x) {
  return(createMassPeaks(mass=x[, 1], 
                         intensity=x[, 2],
                         metaData=x))}

mass=x[, 1] works but mass=mass(x[, 1]) does not, the same for instensity and metaData. Then using David Robinson's approach you should get:

质量=x[, 1]可以工作,但是质量=质量(x[, 1])不行,安装和元数据也是如此。然后使用David Robinson的方法你应该得到:

lapply(list(a,b,c), as.MassPeaks)
[[1]]
S4 class type            : MassPeaks           
Number of m/z values     : 50                  
Range of m/z values      : 889.265 - 1079.764  
Range of intensity values: 9.098e+02 - 1.12e+03

[[2]]
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 904.282 - 1104.358   
Range of intensity values: 9.256e+02 - 1.115e+03

[[3]]
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 855.554 - 1124.883   
Range of intensity values: 8.798e+02 - 1.132e+03

#1


2  

You want to put the data frames into a list, and then use lapply to create a list out of that:

您想要将数据帧放入一个列表,然后使用lapply创建一个列表:

data.frames = list(a, b, c, d)
mass.peaks.each = lapply(data.frames, as.MassPeaks)

Now mass.peaks.each is a list of of the results from each.

现在mass.peaks。每个都是每个结果的列表。

Incidentally, I think you want to change as.MassPeaks to:

顺便说一句,我想你想把a换成a。MassPeaks:

  as.MassPeaks <- function(x) {return(createMassPeaks(mass=mass(x[, 1]), intensity=intensity(x[, 2]),metaData=metaData(x)))}

#2


2  

Your for fails because you forget to use get function

你的for失败是因为你忘记使用get函数

library(MALDIquant)  # You should write what package is used

`Names <- c('a', 'b','c')` # this a vector of names

for(i in Names){
  assign(i, createMassPeaks(mass=get(i)[, 1], intensity=get(i)[, 2]))
}

This loop should work. Let's test it

这个循环。让我们做个测试

set.seed(001)
a <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
b <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
c <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))

The result looks like this:

结果是这样的:

> a
S4 class type            : MassPeaks           
Number of m/z values     : 50                  
Range of m/z values      : 889.265 - 1079.764  
Range of intensity values: 9.098e+02 - 1.12e+03
> b
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 904.282 - 1104.358   
Range of intensity values: 9.256e+02 - 1.115e+03
> c
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 855.554 - 1124.883   
Range of intensity values: 8.798e+02 - 1.132e+03

In spite of this for loop works well, @David Robinson's answer is the best one. I want to say one more thing, I think your function should look like this:

尽管for loop运行良好,但@David Robinson的答案是最好的。我想说的是,你的函数应该是这样的:

as.MassPeaks <- function(x) {
  return(createMassPeaks(mass=x[, 1], 
                         intensity=x[, 2],
                         metaData=x))}

mass=x[, 1] works but mass=mass(x[, 1]) does not, the same for instensity and metaData. Then using David Robinson's approach you should get:

质量=x[, 1]可以工作,但是质量=质量(x[, 1])不行,安装和元数据也是如此。然后使用David Robinson的方法你应该得到:

lapply(list(a,b,c), as.MassPeaks)
[[1]]
S4 class type            : MassPeaks           
Number of m/z values     : 50                  
Range of m/z values      : 889.265 - 1079.764  
Range of intensity values: 9.098e+02 - 1.12e+03

[[2]]
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 904.282 - 1104.358   
Range of intensity values: 9.256e+02 - 1.115e+03

[[3]]
S4 class type            : MassPeaks            
Number of m/z values     : 50                   
Range of m/z values      : 855.554 - 1124.883   
Range of intensity values: 8.798e+02 - 1.132e+03