Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename by list() variable name rather than index in R

Tags:

variables

list

r

I am running regressions over a list() of items using a for loop and I would like to replace the reference to the given list input with the name of that variable rather than its index. For example:

frame <- data.frame(y = rnorm(10), x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10) ) 
x.list <- list(frame$x1,frame$x2,frame$x3)  
fit <- list()

for(i in 1:length(x.list)){ fit[[i]] <- summary(lm(frame$y ~ x.list[[i]]))}         
fit

I would like each item in fit to refer to "x1", "x2", "x3" instead of "x.list[[i]]". Thanks for any thoughts on this.

like image 306
coding_heart Avatar asked Jan 21 '26 16:01

coding_heart


2 Answers

Don't define x.list, just iterate over the names:

fit <- vector("list",3)
for ( i in c("x1","x2","x3") ) fit[[i]] <- summary(lm(frame$y ~ frame[[i]]))

Save lms instead. It's likely that you'll want more than the summary, so just save the lms:

xs     <- c("x1","x2","x3")
xs     <- setNames(xs,xs)
fit_lm <- lapply(xs,function(i)lm(frame$y ~ frame[[i]]))

You can look at summary with lapply(fit_lm,summary), but also look at coefficients, with

sapply(fit_lm,`[[`,"coefficients")
#                    x1         x2          x3
# (Intercept) 0.1417501  0.2974165  0.25085281
# frame[[i]]  0.2318912 -0.1468433 -0.08783857
like image 194
Frank Avatar answered Jan 23 '26 04:01

Frank


If you want a return value, lapply is preferable:

xs <- names(frame)[-1]

setNames(
  lapply(xs, function(x, dat) {
    f <- as.formula(paste("y", x, sep = "~"))
    summary(lm(f, data = dat))
  }, dat = frame), 
  xs)

However, the same strategy would work with a for loop.

like image 39
Roland Avatar answered Jan 23 '26 04:01

Roland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!