[R] Post: R-help : help with loops

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Mar 5 18:07:58 CET 2016


You made it. Next time set your email program to send plain text instead of HTML format to avoid us receiving a corrupted version of what you sent. (This is a plain text mailing list. )

You don't need looping, you need ?merge and ?ifelse.

Df1 <- merge( Df1, Df2, all.x = TRUE, by = c ( "Date", "Cellline" ) )
# should now have a Control column in Df1 that is NA where there was no match
Df1$NewData  <- ifelse( is.na( Df1$Control ), Df1$Data, Df1$Data - Df1$Control )

-- 
Sent from my phone. Please excuse my brevity.

On March 5, 2016 7:31:01 AM PST, Efres B <efresbr at hotmail.com> wrote:
>Hello.
>
> 
>
>Im trying to post the following message in R-Help. 
>
> 
>
>I think I must send it via email to do so, so here it is:
>
> 
>
>Hello, 
>I need help with some code. 
>Basically, I have experiments results which I need to process. 
>
>I have 2 dataframes with 3 columns 
>Df1 <- data.frame (Date, Cellline, Data) 
>Df2 <- data.frame (Date, Cellline, Control) 
>
>I want for the entire column Df1-Data to be subtracted by the
>corresponding
>Df2-Controls which Columns Date and Cellline coincide through some type
>of
>loop. 
>So the structure is: 
>
>Df1$Data <- Df1$Data - Df2$Control [when (Df1$Celline == Df2$Celline &
>Df1$Date == Df2$Date]) 
>
>Each Control has several dozen Data values which are linked through
>Data and
>Celline. Of course, the data.frames are of different dimensions. 
>
>I've tried several approaches and failed miserably. 
>Any suggestions? 
>
>Thank you in advance for any.
>
> 
>
>Thanks.
>
> 
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list