Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Can I use names from a list/dataframe, to be recognised as list/dataframe name within R script for a loop function?

$
0
0

I'd like to use a loop function to recognise names from a list/dataframe as an actual list/dataframe name in the R script (for data analysis or manipulation). I will create some pseudo data to try to help show what i'm trying to do.

Here is code to create 3 lists

height <- sample(120:200,200,TRUE)
weight <- sample(40:140,200,TRUE)
income <- sample(20000:200000,200, TRUE)

This code creates a list containing those list names

vars <- c("height","weight","income")

The code below doesn't run, but I would like to use a loop code like this, where it takes the name from the list position and uses it in script as a list name. Thus it's using the name to calculate the mean, and it's using the name to create a new object.

for (i in 1:3) 
 {mean_**vars[i]** = mean(**vars[i]**) }

The result should be 3 objects "mean_height", "mean_weight", "mean_income" which contain the mean scores

I'm not so much interested in the calculating of mean scores, I'm interested in the ability to use the names from the list. I want to be able to expand this to other analyses that are repetitive.

Apologies if above hasn't been articulated too well, I'm quite new to R, so I hope it makes some sense.

Any help will be most useful, or if you can point me in the right direction that would be great.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>