I'm a Stata power user and R newbie having trouble getting loops to work properly in R. Basically I have a bunch of columns in a dataframe that I want to create a series of pirateplots with.
I tried
mycols <- c("column1","column2","column3")
for (i in mycols){
filename <- paste("~/Desktop/",i,".pdf",sep="")
pdf(filename)
pirateplot(formula = i ~ xvar,
data = df)
dev.off()
}
and have also tried replacing c("column1","column2","column3") with the actual column names, i.e. c(column1,column2,column3) but that doesn't work either. Is there not a simple way to refer to an item in a list? It seems like R treats strings and variable names as different things, reasonably, but I can't seem to find documentation for how to deal with this exceedingly simple case.
In Stata this would be far simpler, something like
foreach v of varlist column1-column3 {
loc filename = "`v'plot"
scatter `v' xvar, name("`filename'")
}
Thanks in advance for your help.