I am working with a large list in R. Each element has 37 columns and more than 100 rows. I am trying to create a new list with the same dimensions but for specific columns I want to calculate a new value using the equation x*0.6/0.02587.
Here is a simplified example of what I'm trying to do
my_list
$data1
X Y Z
1 2 3
2 4 2
3 5 7
new_list
$data1
X Y Z
1 60 90
2 120 60
3 150 210
I tried:
-
out<-lapply(data, FUN = function(x)(x*0.6)/0.02)
But this code applied the function to all columns of all elements in the list.
-
fun=function(x) {x = x*0.6/0.02587} out <- lapply(data , function(x) {x <- fun(x[c(2:37)]); x})
the code 2 did work too but in my new list only has 36 columns each element, it did drop the first column (the column I don't want to change).
Any help how to do that is very much appreciated! Thank you!
CodePudding user response:
You need to save the data to the same columns back. Try -
fun=function(x) {x*0.6/0.02587}
out <- lapply(data , function(x) {x[2:37] <- fun(x[2:37]);x})
out