r - apply a function on columns with specific names -


i new in r. have hundreds of data frames this

id  name    ratio_a ratio_b ratio_c ratio_d aa  abcd    0.09    0.67    0.10    0.14 ab  abce    0.04    0.85    0.04    0.06 ac  abcg    0.43    0.21    0.54    0.14 ad  abcf    0.16    0.62    0.25    0.97 af  abcj    0.59    0.37    0.66    0.07 

this example. number , names of ratio_ columns different between data frames, of them start ratio_. want apply function (for example, log(x)), ratio_ columns without specify column number or whole name.

i know how df df, 1 in example:

a <- function(x) log(x) df_log<-data.frame(df[1:2], lapply(df[3:6], a)) 

but have lot of them, , said number of columns different in each.

any suggestion?

thanks

place datasets in list , loop on list elements

lapply(lst, function(x) {i1 <- grep("^ratio_", names(x));                          x[i1] <- lapply(x[i1], a)                           x}) 

note: no external packages used.

data

lst <- mget(paste0("df", 1:100)) 

Comments