[R] Printing a variable in a loop

Rui Barradas ruipbarradas at sapo.pt
Thu Jun 28 17:58:54 CEST 2012


Hello,

Try the following.


dd <- data.frame(A=letters[1:10], var_1=1:10, var_2=11:20)

index <- grep("var_", names(dd))
for(i in index)
	print(dd[[ i ]])  # or dd[, i]
#
vars <- paste("var", 1:2, sep="_")
for(v in vars)
	print(dd[, v])   # or dd[[ v ]]

There's nothing you can do in Stata you can't in R.
I don't know Stata but I know I'm right.
Almost surely.

Hope this helps,

Rui Barradas

Em 28-06-2012 14:42, kat_the_great escreveu:
> Hi Oliver,
> Thank you for your reply.
>
> I can't use vectors as each var_1, var_2 is a new column/variable, not a new
> observation within a vector.
>
> But thank you,
> Kat
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Printing-a-variable-in-a-loop-tp4634673p4634756.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list