[R] using a variable in a subset of a dataframe

K. Elo maillists at nic.fi
Tue Apr 1 20:53:09 CEST 2008


Henrique Dallazuanna wrote (1.4.2008):
> You can try this:
>
> x <- data.frame()
> for(i in LETTERS[1:5]) x[1:10, i] <- rnorm(10)
> x

Or this:

--- cut here ---
df<-data.frame(0) [obsolet, if df already exists]
for (i in 1:10) { df<-data.frame(cbind(df,0)); names(df)
[ncol(df)]<-as.character(i) }
--- cut here ---

How 'df' changes:
Step 1:
> df
  X0
1  0

Step 2 (for-loop): 
> df
  X0 1 2 3 4 5 6 7 8 9 10
1  0 0 0 0 0 0 0 0 0 0  0

Is this what You are looking for?

Kind regards,
Kimmo



More information about the R-help mailing list