質問

I am having a recurring issue of performing specific tasks on multiple data.frames. Here is my working example data.frame, which was imported from text files.

 cellID         X          Y Area    AVGFP DeviationGFP   AvgRFP DeviationsRFP Slice GUI.ID
1       1  18.20775  26.309859  568 5.389085     7.803248 12.13028      5.569880     0      1
2       2  39.78755   9.505495  546 5.260073     6.638375 17.44505     17.220153     0      1
3       3  30.50000  28.250000    4 6.000000     4.000000  8.50000      1.914854     0      1
4       4  38.20233 132.338521  257 3.206226     5.124264 14.04669      4.318130     0      1
5       5  43.22467  35.092511  454 6.744493     9.028574 11.49119      5.186897     0      1
6       6  57.06534 130.355114  352 3.781250     5.713022 20.96591     14.303546     0      1
7       7  86.81765  15.123529 1020 6.043137     8.022179 16.36471     19.194279     0      1
8       8  75.81932 132.146417  321 3.666667     5.852172 99.47040     55.234726     0      1
9       9 110.54277  36.339233  678 4.159292     6.689660 12.65782      4.264624     0      1
10     10 127.83480  11.384886  569 4.637961     6.992881 11.39192      4.287963     0      1

As previous questions I have posted, there are 40 of these data.frames named slice1...slice40.

What I want to do is add a new column to each of these data.frames that contains the product of AVGFP and Area. I can perform this on one data.frame easily by using

stats[[1]]$totalGFP <- stats[[1]]$AVGFP * stats[[1]]$Area

I am stuck trying to apply this command to every data.frame in stats

I appreciate any and all help. To help moving forward when you post a solution can you please describe the details of the commands used to help me follow along, thank you!

役に立ちましたか?

解決

Like this:

stats <- lapply(stats, transform, totalGFP = AVGFP * Area)

I'll do my best to explain but please refer to ?lapply and ?transform for the full docs.

transform is a function to add columns to a data.frame, according to formulas of the type totalGFP = AVGFP * Area passed as arguments. For example, to add the totalGFP column to your first data.frame, you could run transform(stats[[1]], totalGFP = AVGFP * Area).

lapply applies a function (here transform) to each element of a list or a vector (here stats), and returns a list. If the function to be applied requires more arguments, they can be passed at the end of the lapply call, here totalGFP = AVGFP * Area. So here lapply is an elegant way of running transform on each element of stats.

他のヒント

Given that you wrote "please describe the details of the commands", try this simple example:

# create two small data frames
df1 <- data.frame(AVGFP = 1:3, Area = 4:6)
df2 <- data.frame(AVGFP = 7:9, Area = 1:3)

# create a list with named objects: the two data frames.
# ?list: "The arguments to list [...] of the form [...] tag = value
ll <- list(df1 = df1, df2 = df2)
str(ll)

# apply a function on each element in the list
# each element is a single data frame
# Use an 'anonymous function', function(x), where 'x' corresponds to each single data frame
# The function does this:
# (1) calculate the new variable 'total', and (2) add it to the data frame 
ll2 <- lapply(X = ll, FUN = function(x){
  total <- x$AVGFP * x$Area
  x <- data.frame(x, total)
  })

# check ll2
str(ll2)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top