[R] sample

Wu Gong ghowoo at gmail.com
Tue May 18 16:02:50 CEST 2010


Sorry, I made two mistakes. The first was matching the female with the male.
The second was 2 variables should be selected randomly every time.

Followed is a revised copy:

## Import data.
moms <- read.delim("females.txt", sep =" ", stringsAsFactors = FALSE, header
= TRUE)
dads <- read.delim("males.txt", sep =" ", stringsAsFactors = FALSE, header =
TRUE) 

## Mate.
## Each male doesn't mate twice.
parents <- cbind(moms, dads[sample(nrow(dads), nrow(moms)),])

## Assign output data frame.
## The matrix "resultscheck" will be used to check the random selections.
output_offspring <- as.data.frame(matrix("", nrow = nrow(moms), ncol = 6),
stringsAsFactors = FALSE) 
resultscheck <- as.data.frame(matrix("", nrow = nrow(moms), ncol = 6),
stringsAsFactors = FALSE) 

## Randomly select two variables both from moms and dads.
for(i in 1:nrow(parents)) {
	selection <- c(1, sample((2:5),2), 6, sample((7:10),2)) 
	output_offspring[i,] <- parents[1,selection]
	resultscheck[i,] <- selection
	}
	
## Show the random selections.
resultscheck

## Output.
write.table(output_offspring,"offspring_7.txt",row.names=F,col.names=c("momID","A1","A2","dadID","A3","A4"),quote=F) 



-----
A R learner.
-- 
View this message in context: http://r.789695.n4.nabble.com/sample-tp2218361p2221328.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list