Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-02 Thread Petr PIKAL
Hi

my favourite would be

test$v[which(test$pattern==1)]-NA

Regards
Petr


 Hi,
 
 On Mar 1, 2012, at 12:38 PM, Sarah Goslee wrote:
 
  Hi,
  
  On Thu, Mar 1, 2012 at 11:11 AM, mails mails00...@gmail.com wrote:
  Hello,
  
  
  consider the following data.frame:
  
  test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
  c(1,1,NA,1,NA))
  
 
  snip 
 
  So basically the result should look like this:
  test
   n v pattern
  1  1 NA   1
  2  2 NA  1
  3  3 7  NA
  4  4 NA   1
  5  5 3  NA
  
  So far, I solved it by creating subsets and using merge but it turns 
out to
  be super slow. Is there a way to do that
  with the apply function?
  
  Far too much work. What about:
  
  test$v - ifelse(test$pattern == 1, NA, v)
  test
   n  v pattern
  1 1 NA   1
  2 2 NA   1
  3 3 NA  NA
  4 4 NA   1
  5 5 NA  NA
 
 Actually that doesn't work because of those pesky missing values. You 
need
 
 test -  transform(test, v = ifelse(pattern == 1  !is.na(pattern), NA, 
v))
 
 Best,
 Ista
 
  
  
  -- 
  Sarah Goslee
  http://www.functionaldiversity.org
  
  __
  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.

__
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] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread mails
Hello,


consider the following data.frame:

test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
c(1,1,NA,1,NA))

 test
  n v pattern
1  1 6   1
2  2 5   1
3  3 7  NA
4  4 5   1
5  5 3  NA


I tried to use apply and the adply function to set v to NA where pattern = 1
and v to v where pattern = 1


So basically the result should look like this:
 test
  n v pattern
1  1 NA   1
2  2 NA  1
3  3 7  NA
4  4 NA   1
5  5 3  NA

So far, I solved it by creating subsets and using merge but it turns out to
be super slow. Is there a way to do that
with the apply function?

Any help/hint is appreciated

Thanks


--
View this message in context: 
http://r.789695.n4.nabble.com/Delete-rows-from-data-frame-matching-a-certain-criteria-tp4435414p4435414.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread Sarah Goslee
Hi,

On Thu, Mar 1, 2012 at 11:11 AM, mails mails00...@gmail.com wrote:
 Hello,


 consider the following data.frame:

 test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
 c(1,1,NA,1,NA))

 test
  n v pattern
 1  1     6       1
 2  2     5       1
 3  3     7      NA
 4  4     5       1
 5  5     3      NA


Thanks for the reproducible example.

 I tried to use apply and the adply function to set v to NA where pattern = 1
 and v to v where pattern = 1

Presumably that should be set v to NA where pattern == 1 and v to v where
pattern != 1


 So basically the result should look like this:
 test
  n v pattern
 1  1     NA       1
 2  2     NA      1
 3  3     7      NA
 4  4     NA       1
 5  5     3      NA

 So far, I solved it by creating subsets and using merge but it turns out to
 be super slow. Is there a way to do that
 with the apply function?

Far too much work. What about:

 test$v - ifelse(test$pattern == 1, NA, v)
 test
  n  v pattern
1 1 NA   1
2 2 NA   1
3 3 NA  NA
4 4 NA   1
5 5 NA  NA


-- 
Sarah Goslee
http://www.functionaldiversity.org

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


Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread jim holtman
Your criteria did not make sense since in both cases pattern == 1, so
I chose to set to NA if pattern == 1

 test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
+ c(1,1,NA,1,NA))
 test
  n v pattern
1 1 6   1
2 2 5   1
3 3 7  NA
4 4 5   1
5 5 3  NA
 # set v to NA when pattern = 1
 test$v[!is.na(test$pattern)  (test$pattern == 1)] - NA
 test
  n  v pattern
1 1 NA   1
2 2 NA   1
3 3  7  NA
4 4 NA   1
5 5  3  NA




On Thu, Mar 1, 2012 at 11:11 AM, mails mails00...@gmail.com wrote:

 Hello,


 consider the following data.frame:

 test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
 c(1,1,NA,1,NA))

  test
  n v pattern
 1  1     6       1
 2  2     5       1
 3  3     7      NA
 4  4     5       1
 5  5     3      NA


 I tried to use apply and the adply function to set v to NA where pattern =
 1
 and v to v where pattern = 1


 So basically the result should look like this:
  test
  n v pattern
 1  1     NA       1
 2  2     NA      1
 3  3     7      NA
 4  4     NA       1
 5  5     3      NA

 So far, I solved it by creating subsets and using merge but it turns out
 to
 be super slow. Is there a way to do that
 with the apply function?

 Any help/hint is appreciated

 Thanks


 --
 View this message in context:
 http://r.789695.n4.nabble.com/Delete-rows-from-data-frame-matching-a-certain-criteria-tp4435414p4435414.html
 Sent from the R help mailing list archive at Nabble.com.

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




--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

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


Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread Nordlund, Dan (DSHS/RDA)
 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of mails
 Sent: Thursday, March 01, 2012 8:11 AM
 To: r-help@r-project.org
 Subject: [R] Delete rows from data.frame matching a certain criteria
 
 Hello,
 
 
 consider the following data.frame:
 
 test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
 c(1,1,NA,1,NA))
 
  test
   n v pattern
 1  1 6   1
 2  2 5   1
 3  3 7  NA
 4  4 5   1
 5  5 3  NA
 
 
 I tried to use apply and the adply function to set v to NA where
 pattern = 1
 and v to v where pattern = 1
 
 
 So basically the result should look like this:
  test
   n v pattern
 1  1 NA   1
 2  2 NA  1
 3  3 7  NA
 4  4 NA   1
 5  5 3  NA
 
 So far, I solved it by creating subsets and using merge but it turns
 out to
 be super slow. Is there a way to do that
 with the apply function?
 
 Any help/hint is appreciated
 
 Thanks
 
 

There is no need for apply here, this is a simple indexing problem.  Something 
like this should work

test$v - ifelse(is.na(test$pattern), test$v, NA)


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


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


Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread Ista Zahn
Hi,

On Mar 1, 2012, at 12:38 PM, Sarah Goslee wrote:

 Hi,
 
 On Thu, Mar 1, 2012 at 11:11 AM, mails mails00...@gmail.com wrote:
 Hello,
 
 
 consider the following data.frame:
 
 test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
 c(1,1,NA,1,NA))
 

 snip 

 So basically the result should look like this:
 test
  n v pattern
 1  1 NA   1
 2  2 NA  1
 3  3 7  NA
 4  4 NA   1
 5  5 3  NA
 
 So far, I solved it by creating subsets and using merge but it turns out to
 be super slow. Is there a way to do that
 with the apply function?
 
 Far too much work. What about:
 
 test$v - ifelse(test$pattern == 1, NA, v)
 test
  n  v pattern
 1 1 NA   1
 2 2 NA   1
 3 3 NA  NA
 4 4 NA   1
 5 5 NA  NA

Actually that doesn't work because of those pesky missing values. You need

test -  transform(test, v = ifelse(pattern == 1  !is.na(pattern), NA, v))

Best,
Ista

 
 
 -- 
 Sarah Goslee
 http://www.functionaldiversity.org
 
 __
 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.


Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread Sarah Goslee
You're all correct: I copied in the wrong thing. My apologies!

On Thu, Mar 1, 2012 at 1:00 PM, Ista Zahn istaz...@gmail.com wrote:
 Hi,

 On Mar 1, 2012, at 12:38 PM, Sarah Goslee wrote:

 Hi,

 On Thu, Mar 1, 2012 at 11:11 AM, mails mails00...@gmail.com wrote:
 Hello,


 consider the following data.frame:

 test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
 c(1,1,NA,1,NA))


  snip 

 So basically the result should look like this:
 test
  n v pattern
 1  1     NA       1
 2  2     NA      1
 3  3     7      NA
 4  4     NA       1
 5  5     3      NA

 So far, I solved it by creating subsets and using merge but it turns out to
 be super slow. Is there a way to do that
 with the apply function?

 Far too much work. What about:

 test$v - ifelse(test$pattern == 1, NA, v)
 test
  n  v pattern
 1 1 NA       1
 2 2 NA       1
 3 3 NA      NA
 4 4 NA       1
 5 5 NA      NA

 Actually that doesn't work because of those pesky missing values. You need

 test -  transform(test, v = ifelse(pattern == 1  !is.na(pattern), NA, v))

 Best,
 Ista



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


Re: [R] Delete rows from data.frame matching a certain criteria

2012-03-01 Thread David Winsemius


On Mar 1, 2012, at 1:02 PM, Sarah Goslee wrote:


You're all correct: I copied in the wrong thing. My apologies!

On Thu, Mar 1, 2012 at 1:00 PM, Ista Zahn istaz...@gmail.com wrote:

Hi,

On Mar 1, 2012, at 12:38 PM, Sarah Goslee wrote:


Hi,

On Thu, Mar 1, 2012 at 11:11 AM, mails mails00...@gmail.com wrote:

Hello,


consider the following data.frame:

test - data.frame(n = c(1,2,3,4,5), v = c(6,5,7,5,3), pattern =
c(1,1,NA,1,NA))



 snip 


So basically the result should look like this:

test

 n v pattern
1  1 NA   1
2  2 NA  1
3  3 7  NA
4  4 NA   1
5  5 3  NA


So far, I solved it by creating subsets and using merge but it  
turns out to

be super slow. Is there a way to do that
with the apply function?


Far too much work. What about:


test$v - ifelse(test$pattern == 1, NA, v)
test

 n  v pattern
1 1 NA   1
2 2 NA   1
3 3 NA  NA
4 4 NA   1
5 5 NA  NA


Actually that doesn't work because of those pesky missing values.  
You need


test -  transform(test, v = ifelse(pattern == 1  !is.na(pattern),  
NA, v))



What about just using `is.na-`:

is.na(test$v) - test$pattern==1



--
David Winsemius, MD
West Hartford, CT

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