Dear R community, I am using a dataset and would like to define new
variables using a R variable:
e.g.
for (i in 1:10){
dataset$i<-something
}
But this is not the right way, I get only one variable in "dataset"...
How can I change this?
Thank you!
Georg.
************************
Georg Ehret
Johns Hopkins
Baltimore, MD
[[alternative HTML version deleted]]
Henrique Dallazuanna
2008-Apr-01 18:33 UTC
[R] using a variable in a subset of a dataframe
You can try this: x <- data.frame() for(i in LETTERS[1:5]) x[1:10, i] <- rnorm(10) x On 01/04/2008, Georg Ehret <georgehret at gmail.com> wrote:> Dear R community, I am using a dataset and would like to define new > variables using a R variable: > > e.g. > > for (i in 1:10){ > dataset$i<-something > } > > But this is not the right way, I get only one variable in "dataset"... > How can I change this? > > Thank you! > Georg. > ************************ > Georg Ehret > Johns Hopkins > Baltimore, MD > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
i m not sure what u really want to do but if ur looking for some subsample
of dataset then u might wanna try this (say in case u want to resample ur
dataset by column "id" in ur dataset then do
resample<-sample(dataset$id,n,replace=F)
dataset<-dataset[resample, ]
note here n is the size of subset u wanna sample from ur dataset
hope this helps.
cheers
tasneem zaihra
On Tue, 1 Apr 2008 14:23:10 -0400 "Georg Ehret" wrote:
> Dear R community, I am using a dataset and would like to define new
> variables using a R variable:
>
> e.g.
>
> for (i in 1:10){
> dataset$i<-something
> }
>
> But this is not the right way, I get only one variable in
"dataset"...
> How can I change this?
>
> Thank you!
> Georg. > ************************
> Georg Ehret
> Johns Hopkins
> Baltimore, MD
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.