here is my original data frame
a b c A B C
[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12
[3,] 13 14 15 16 17 18
a, b, c are type I variables
A, B, C are type II variables
how can I create a new data frame in which:
1) in each row, there are one random number from type I variables, and two
random numbers from type II variables
2) meanwhile, in each row, the two type II numbers have to be only those
numbers that are not corresponding to the type I number. For example, if the
type I number is 1, the type II numbers should not include 4.
the new data frame should be like this:
[,1] [,2] [,3]
[1,] I II II
[2,] I II II
[3,] I II II
Many thanks!! :)
--
View this message in context:
http://r.789695.n4.nabble.com/use-sample-function-to-create-new-data-frame-tp3052110p3052110.html
Sent from the R help mailing list archive at Nabble.com.
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.
Gong, thanks!! but your syntax did work out as I would expect. here following are the reasons: each row from my original data frame represents data from one subject. when the one type I number and the two type II numbers are drawn from the original data frame, both type I number and type II numbers should both from the same subject. by doing so, the new data frame should only have 3 rows with each representing one subject. I am trying to revise your syntax to make it work... :) -- View this message in context: http://r.789695.n4.nabble.com/use-sample-function-to-create-new-data-frame-tp3052110p3052629.html Sent from the R help mailing list archive at Nabble.com.