1

I quite a few data sets that I would like to plot using a for loop. I make a list using

dflist <- c('Set1', 'Set2','Set3', 'Set4')

Then I try to do:

for (i in dflist) {
   plot(i$var1,i$var2)
}

Which does not work. Could anyone sort me out here?

1
  • i$var1 throws an exception. You need a better reproducible example Commented Jan 27, 2015 at 14:14

2 Answers 2

1

Try using dflist<-list(set1,set2) otherwise you get a list of individual var1 and var2 vectors in dflist

Sign up to request clarification or add additional context in comments.

Comments

1

You could use get for this, which 'gets' the object associated with the supplied name.

# two data frames
a = data.frame(x=1:10,y=sqrt(1:10)); b=data.frame(x=1:10,y=exp(1:10))

par(mfrow=c(2,1))
dfs = c('a', 'b')
for (i in dfs){
    plot(get(i)$x, get(i)$y, type='o')
}

Or apply to avoid a loop, applying a custom function to each item in the list.

par(mfrow=c(2,1))
dflist = list(a,b)
lapply(X=dflist, FUN = function(x) plot(x$x, x$y, type='o')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.