[R] creating a variable using concatenation
Peter Alspach
Peter.Alspach at plantandfood.co.nz
Wed Mar 31 04:12:58 CEST 2010
Tena koe Zubin
Would this work for your application:
toPlot <- paste("X",i,sep="")
plot(x[, toPlot])
HTH ....
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of zubin
> Sent: Wednesday, 31 March 2010 2:25 p.m.
> To: r-help at r-project.org
> Subject: [R] creating a variable using concatenation
>
> A general problem i run into, i know there must be a simple solution.
>
> I like to create a variable by appending a 1 for example, (i need to
> loop later on from 1 to X, thus the reason for this). So i assign
the
> variable vplot with this value, however it has quotes and when i use
it
> in a barplot, it throws an error. but the tcenter$X1 does exist, its
> an
> element of a data frame. So if i type directly it works, but i like
to
> do this programmatically, as i have to generate a bunch of these plots
> and need to loop.
>
> So how do i concatenate to create a variable, then reference that
> variable in a function call?
>
>
> R> x <- data.frame(1,2,3,4,5,6,7,8)
> R> x
> X1 X2 X3 X4 X5 X6 X7 X8
> 1 1 2 3 4 5 6 7 8
> R> x$X1
> [1] 1
>
> R> i=1
> R> toplot <- paste("x$X",i,sep="")
> R> toplot
> [1] "x$X1"
>
> okay lets test:
>
> R> plot(x$X1)
> -it works i see the plot
>
> however this DOES not work
>
> R> plot(toplot)
>
> Error in plot.window(...) : need finite 'ylim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by
coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
>
>
> Thus, that's my problem, i know it must be simple - the variable is
> equal to x$X1 but it does not work in a function call? i tried many
> functions - always some type of error.
>
> ______________________________________________
> 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