Hi,

Your description isn't clear:

On Thursday, July 24, 2014, Matthew <mccorm...@molbio.mgh.harvard.edu>
wrote:

> I am coming from the perspective of Excel and VBA scripts, but I would
> like to do the following in R.
>
>  I have a data frame with 14 columns and 32,795 rows.
>
> I want to check the value in column 8 (row 1) to see if it is a 0.
> If it is not a zero, proceed to the next row and check the value for
> column 8.
> If it is a zero, then
> a) change the zero to a 1,
> b) divide the value in column 9 (row 1) by 1,


Row 1, or the row in which column 8 == 0?
Why do you want to divide by 1?


> c) place the result in column 10 (row 1) and


Ditto on the row 1 question.
What do you want column 10 to be if column 8 isn't 0? Does it already have
a value. I suppose it must.


> d) repeat this for each of the other 32,794 rows.
>
> Is this possible with an R script, and is this the way to go about it. If
> it is, could anyone get me started ?


Assuming you want to put the new values in the rows where column 8 == 0,
you can do it in two steps:

mydata[,10] <- ifelse(mydata[,8] == 0, mydata[,9]/whatever, mydata[,10])
#where whatever is the thing you want to divide by that probably isn't 1
mydata[,8] <- ifelse(mydata[,8] == 0, 1, mydata[,8])

R programming is best done by thinking about vectorizing things, rather
than doing them in loops. Reading the Intro to R that comes with your
installation is a good place to start.

Sarah


>
> Matthew
>
>
>

-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to