On 2010-05-16 2:23, Laetitia Schmid wrote:
Hi,
I am sampling two random columns from females and two random columns
from males to produce tetraploid offspring. For every female I am
sampling a random male.
In the end I want to write out a a matrix with all the offspring, but
that does not work. I get always only the offspring from the last
females. There must be a mistake in my script:
moms<-read.delim("females.txt", stringsAsFactors=FALSE,header=TRUE)
dads<-read.delim("males.txt", stringsAsFactors=FALSE,header=TRUE)
output_offspring<-data.frame()
for (i in 1:nrow(moms)){
rdad=sample(1:nrow(dads),1)
kid<-c(sample(moms[i,2:5],2),sample(dads[rdad,2:5],2))
output_offspring<-rbind(output_offspring,c(moms$SampleID[i],dads$SampleID[rdad],kid))
}
(When I run your code, I get an error.)
It's always best to pre-assign your output to have the
desired dimensions and then fill in the cells:
output_offspring <- as.data.frame(matrix("", nrow=nrow(moms), ncol=6),
stringsAsFactors=FALSE)
for (i in 1:nrow(moms)){
rdad <- sample(1:nrow(dads),1)
kid <- c(sample(moms[i,2:5],2), sample(dads[rdad,2:5],2))
output_offspring[i,] <- c(moms$SampleID[i], dads$SampleID[rdad], kid)
}
Personally, I would work with matrices, since all of your data
are string variables.
-Peter Ehlers
write.table(output_offspring,"offspring_7.txt",row.names=T,col.names=T,quote=F)
females.txt:
SampleID A1 A2 A3 A4
GM920222 GATTGCC GATTGCC GATAGAC GATAGAC
GM930040 GTCATCA GAGTGCA ACTATAA GATTGCC
GM930040 GTCATCA GAGTGCA ACTATAA GATTGCC
GM960023 GATTGCC GTCATCA GATTGCC GATTGCC
GM920224 ACTAGAA GTCATCA GTCATCA ACTAGAA
GM920224 ACTAGAA GTCATCA GTCATCA ACTAGAA
GM920034 GATTGCC GTCATCA GATTGCA GATTGCA
GM920096 GATTGCC GATTGCC GATTGCA GATTGCC
GM930029 GTCATCA GATTGCC GTCATCA GATTGCC
GM940031 GATTGCC GAGTGCA GATTGCA ACTAGAA
GM960028 GATTGCC GAGTGCA GATTGCA ACTAGAA
GM980007 GTCATCA GATTGCC ACTTGAA GTCATCA
GM970009 ACTAGAA GTCAGAA GTCAGCA ACTAGCA
GM930026 ACTAGAA GAGTGCA GAGTGCA ACTAGAA
GM920031 GATTGCC GTCATCA GATTGCC GATTGCC
GM990105 GATTGCC GATTGCC GTCAGCA GTCAGCA
GM920202 GATTGCC GATTGCC GATTGCC GATTGCC
GM920089 GAGTGCA GTCAGAA ACTATCA GATTGCC
GM980051 ACTAGAA ACTAGAA GATAGCC GATAGCC
GM930109 GTCATCA GAGTGCA GTTTTAA ACTAGAA
GM940039 GTCATCA GAGTGCA GTTTGCC ACTTTCA
GM050099 GAGTGCA GTCAGAA GTTATCC ACTTTCA
GM050099 GAGTGCA GTCAGAA GTTATCC ACTTTCA
GM030005 ACTAGAA GAGTGCA ACTAGAA ACTAGAA
GM050009 ACTAGAA GATTGCC GATTGCC ACTAGAA
GM990027 GATTGCC GAGTGCA GATTGCA GATTGCC
GM990066 GATTGCC GTCATCA GTCATCA GATTGCC
males.txt:
SampleID A1 A2 A3 A4
WI920425 ACTAGAA ACCATCA ACTAGAA ACTAGAA
WI920408 ACTAGAA ACTAGAA ACTAGAA ACTAGAA
WI920009 ACTAGAA ACTAGAA ACTAGAA GATTGCC
WI920352 ACTTTCA ACGTTCA GAGAGAA GATTGCA
WI920004 GATTGCC GATTGCC ACTAGAA ACTAGAA
WI920353 ACTAGAA GATTGCC ACTAGAA GATTGCC
WI920410 ACTAGAA GTCAGAA GAGTACC ACTTTCA
WI920007 ACTAGAA ACTTTCA GAATGCA GTTAGAC
WI920015 ACTTTCA ACGTTCA GTCAGAA GATTGCC
WI920426 ACTAGAA GTCATCA GTCATCA ACTAGAA
WI920433 ACTAGAA GTCAGAA GTCTGCA ACTTGCA
WI920370 GATTGCC GAGTGCA GATTGCA ACTAGAA
WI920437 GTCATCA GTCAGAA GATTGCC ACTTTCA
WI920027 GATTGCC GAGTGCA GATTACC GATTGCC
WI920415 GATTGCC GAGTGCA GTCATCA ACTAAAA
WI920023 ACTTTCA GTCAGAA GAGATCA GATTGCC
WI920360 GATTGCC GTCATCA GATTGCA ACTTTCA
WI920017 GATTGCC GTCAGAA GATTTCC ACTAGCA
WI920028 GTCATCA GTCAGAA GATTGCC ACTTGCA
WI920361 GATTGCC GAGTGCA GTCAGCA GATTGCC
WI920367 GATTGCC GATTGCC GTCATCA GATTGCC
WI920366 GATTGCC GATTGCC GTCTGCA GTCTGCA
WI920365 GATTGCC GAGTGCA GTCAGCA GTTTGCC
WI920362 GATTGCC GAGTGCA GATTGCA ACTAGAA
WI920441 GATTGCC GAGTGCA GATTGCA ACTAGAA
WI920022 GTCATCA GTCAGAA GATTGCC ACTTTCA
WI920356 GTCATCA GTCAGAA GATTGCC ACTTTCA
WI920355 GATTGCC GATTGCC GATTGCC GATTGCC
WI920423 GATTGCC GATTGCC GATTGCC GATTGCC
WI920021 GATTGCC GATTGCC GTCAGCA GATTGCC
WI920359 GATTGCC GATTGCC GTCAGCA GATTGCC
WI920024 GATTGCC GATTGCC GATTGCC GATTGCC
WI920369 GATTGCC GATTGCC GATTGCC GATTGCC
WI920416 GATTGCC GATTGCC GATTGCC GATTGCC
WI920427 GATTGCC GATTGCC GATTGCC GATTGCC
WI920428 GATTGCC GATTGCC GATTGCC GATTGCC
WI920431 GATTGCC GATTGCC GATTGCC GATTGCC
WI920001 GATTGCC GTCATCA GTCATCA GATTGCC
WI920010 GATTGCC GTCATCA GTCATCA GATTGCC
WI920349 GATTGCC GTCATCA GTCATCA GATTGCC
WI920363 GATTGCC GTCATCA GTCATCA GATTGCC
WI920417 GATTGCC GTCATCA GTCATCA GATTGCC
WI920430 GATTGCC GTCATCA GTCATCA GATTGCC
______________________________________________
R-help@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.
______________________________________________
R-help@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.