-2

I have posted a similar question to this before and got a quick answer, but am just an R beginner and haven't been able to adapt it to what I need.

Basically I want to take the below code (says if Date_Index is between two numbers and df is < X, then turn df to Y) and make it so it only applies to entries that meet a certain criteria, i.e:

HAVE: df[df$Date_Index >= 50 & df$Date_Index <= 52 & df < .0000001]=1

ADD: if df$Date_Index <= 49 AND df = 0.00 ignore the above statement, else execute:

In other words I need the equivalent to an if, then, else clause. If Date_Index <= 49 and df = 0, leave alone, else if Date_Index >=50 and Date Index <= 52 and df < .001 then replace data (in Date Index rows 50-52) with 1.

This (simple) data set should illustrate it enough:

xx <- matrix(0,52,5)
xx[,1]=1
xx[,3]=1
xx[,5]=1
xx[50:52,]=0
xx[,1]=1:52
xx[50,3]=1

So what I'd like is column 2 and column 4 to stay all 0's but for the bottom of column 3 and 5 to continue to be all 1's.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Austin
  • 73
  • 1
  • 7
  • 3
    Your HAVE doesn't look right. Seems you wanted to index the rows yet you are indexing elements. Also, you are not indexing the variable B, you just index the *entire* data frame (df < .0000001), and you don't reference variable Y to assign to. This is why we ask for reproducible examples as I don't see how HAVE was even working. As such it is hard to proceed to the next step and add in the extra clause. Either reference the earlier question or add a complete, small, reproducible example here. – Gavin Simpson Jan 07 '14 at 18:15
  • 1
    I think you are missing the piece of df that should be compared. df < .0000001. Also. Be very careful with df=0.0; I hope you know the difference between df==0.0 and df=0. A good trick is to write 0==df when in doubt – Wilmer E. Henao Jan 07 '14 at 18:23
  • Sorry, unable to share my code due to privacy of data, I tried to edit the original question to help explain that I need the syntax for an if, then, else clause. Sorry if I'm being confusing, but I do appreciate your time. – Austin Jan 07 '14 at 18:26
  • Wilmer - noted. I basically can just use df < .00001 as I have before I suppose. Thanks – Austin Jan 07 '14 at 18:27
  • You do not need to provide your actual data. Just create a minimal reproducible example. – Sven Hohenstein Jan 07 '14 at 18:30
  • How to make small, reproducible data sets: [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), and [**here**](http://stackoverflow.com/questions/10454973/how-to-create-example-data-set-from-private-data-replacing-variable-names-and-l) – Henrik Jan 07 '14 at 18:31
  • Okay, I posted an example of a 0,1 matrix with column 1 of 1:52. – Austin Jan 07 '14 at 19:02
  • Why do columns 2 and 4 stay all 0s? `0 < .0000001` – Sven Hohenstein Jan 07 '14 at 19:09
  • The < .00001 was originally written to help keep 1's going all the way to row 52, but then I realized for the whole columns that were blank would also turn to 1, when they were not intended to. – Austin Jan 07 '14 at 19:13
  • What I have now would make row 50-52 1's for all columns, that's the extra step I can't get. – Austin Jan 07 '14 at 19:14
  • 1
    Do you want to compare with `0` in all rows of 1 to 49? – Sven Hohenstein Jan 07 '14 at 19:24
  • So basically with the way the data is, if any of [45:49,] are 0, that would mean the entire column is blank. Just need to check if any of those are 0, and if they are I won't want to fill in the 1's. – Austin Jan 07 '14 at 19:27

1 Answers1

0

I suppose you're looking for this:

xx[xx[,1] >= 50 & xx[,1] <= 52, c(FALSE, !colSums(!xx[xx[,1] <= 49, -1]))] <- 1
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • thanks so much, help is much appreciated as I get up to speed with R, will try to start more specific next time. – Austin Jan 07 '14 at 19:33