[R] Compare data in two rows and replace objects in data frame
Gerrit Eichner
Gerrit.Eichner at math.uni-giessen.de
Mon Aug 4 12:47:18 CEST 2014
Hello, Raz,
if X is the data frame that contains your data, then using sort of an
"indexing trick" to circumvent your numerous if-statements as in
aggregate( X[ c( "genotype 2001", "genotype 2002", "genotype 2003")],
X[ "CloneID"],
FUN = function( x)
c( "11" = "HT",
"10" = "A",
"01" = "B",
"1-" = "Aht",
"-1" = "Bht")[ paste( x, collapse = "")])
presumably does what you want (and can certainly be improved).
Hth -- Gerrit
---------------------------------------------------------------------
Dr. Gerrit Eichner Mathematical Institute, Room 212
gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner
---------------------------------------------------------------------
On Mon, 4 Aug 2014, raz wrote:
> Dear all,
>
> I have a data frame 144 x 20000 values.
> I need to take every value in the first row and compare to the second row,
> and the same for rows 3-4 and 5-6 and so on.
> the output should be one line for each of the two row comparison.
> the comparison is:
> if row1==1 and row2==1 <-'HT'
> if row1==1 and row2==0 <-'A'
> if row1==0 and row2==1 <-'B'
> if row1==1 and row2=='-' <-'Aht'
> if row1=='-' and row2==1 <-'Bht'
>
> for example:
> if the data is:
> CloneID genotype 2001 genotype 2002 genotype 2003
> 2471250 1 1 1
> 2471250 0 0 0
> 2433062 0 0 0
> 2433062 1 1 1
> 100021605 1 1 0
> 100021605 1 0 1
> 100005599 1 1 0
> 100005599 1 1 1
> 100002798 1 1 0
> 100002798 1 1 1
>
> then the output should be:
> CloneID genotype 2001 genotype 2002 genotype 2003
> 2471250 A A A
> 2433062 B B B
> 100021605 HT A B
> 100005599 HT HT B
> 100002798 HT HT B
>
> I tried this for the whole data, but its so slow:
>
> AX <- data.frame(lapply(AX, as.character), stringsAsFactors=FALSE)
>
>
> for (i in seq(1,nrow(AX),by=2)){
> for (j in 6:144){
> if (AX[i,j]==1 & AX[i+1,j]==0){
> AX[i,j]<-'A'
> }
> if (AX[i,j]==0 & AX[i+1,j]==1){
> AX[i,j]<-'B'
> }
> if (AX[i,j]==1 & AX[i+1,j]==1){
> AX[i,j]<-'HT'
> }
> if (AX[i,j]==1 & AX[i+1,j]=="-"){
> AX[i,j]<-'Aht'
> }
> if (AX[i,j]=="-" & AX[i+1,j]==1){
> AX[i,j]<-'Bht'
> }
> }
> }
>
> AX1<-AX[!duplicated(AX[,3]),]
> AX2<-AX[duplicated(AX[,3]),]
>
> Thanks for any help,
>
> Raz
>
>
>
> --
> \m/
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list