[R] use sample function to create new data frame
Wu Gong
wg2f at mtmail.mtsu.edu
Sun Nov 21 07:12:39 CET 2010
Hi,
I would try to sample by row index and column index. Hope it helps.
df <- data.frame(matrix(1:18,nrow=3,ncol=6,byrow=TRUE))
colnames(df) <- c(letters[1:3],LETTERS[1:3])
## Generate a new data frame with 10 rows
one <- two <- three <- numeric(10)
for (i in 1:10) {
## Sample from type I
col.1 <- sample(1:3,1)
row.1 <- sample(1:nrow(df),1)
one[i] <- df[row.1,col.1]
## Sample from type II
col.2 <- sample(setdiff(4:6,col.1+3),2,replace=TRUE)
row.2 <- sample(1:nrow(df),2,replace=TRUE)
two[i] <- df[row.2[1],col.2[1]]
three[i] <- df[row.2[2],col.2[2]]
}
new.data <- data.frame(one, two, three)
-----
A R learner.
--
View this message in context: http://r.789695.n4.nabble.com/use-sample-function-to-create-new-data-frame-tp3052110p3052159.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list