user6644063 - 2 months ago 8x

R Question

I have got a list with 10 data.frames and I just need to divide two columns for each data.frame and after calculate the relative standard deviation.

I would like to use lapply.

Here an example of one of the data.frame contained within the list:

`df <- read.table(text = 'X Y`

2 4

5 3

1 2

7 1

4 2

6 1', header = TRUE)

I have to perform the following operations with lapply for all my 10 data.frames:

`ratio <- df$X / df$Y`

sd <- sd(ratio)

Answer

We can do this by looping over the `list`

with `lapply`

, extract the columns of interest, divide to get the 'ratio' and then do the `sd`

on that ratio. (It could be done on a single step too)

```
lapply(lst, function(x) {ratio <- x$X/x$Y
sd(ratio) })
```

where 'lst' is the `list`

of 'data.frame's.

Source (Stackoverflow)

Comments