[BioC] splitting a data frame
Seth Falcon
sfalcon at fhcrc.org
Thu Apr 26 21:50:33 CEST 2007
<kfbargad at ehu.es> writes:
> Dear list,
>
> sorry for this very basic question but I have searched the R manual
> without success. What function do I need to randomly split a
> data.frame into two data.frames (for example a data.frame containing
> 200 rows and 5 columns into two of say 150 and 50 rows each)? Should I
> first create a vector of 150 random numbers between 1 and 200 (but
> how?) and then use subset() function?
This is not a BioC specific question and would be better asked on the
R-help list.
Yes, you can do this by creating a vector of row indices. See
help("sample"). You don't need subset. Just "[".
v = sample(1:nrow(df))
df[v[1:50], ]
df[v[51:nrow(df)], ]
+ seth
--
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org
More information about the Bioconductor
mailing list