[R] Remove specific rows in a matrix/data.frame

2011-10-13 Thread syrvn
Hi,


imagine the following matrix/data.frame

Letter Number
a 1
a 1
b 1
b 0
c 0
c 1
d 0
d 0 

If the numbers for two identical letters are also identical then I want to
remove either the first or the
second row of that letter. If for a letter the numbers are 1 and 0 I want to
remove the row with the 0.

That means if the code works I would and up with the following
matrix/data.frame

Letter Number
a 1
b 1
c 1
d 1


Many thanks,
Syrvn


--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902149.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] Remove specific rows in a matrix/data.frame

2011-10-13 Thread David Winsemius


On Oct 13, 2011, at 12:42 PM, syrvn wrote:


Hi,


imagine the following matrix/data.frame

Letter Number
a 1
a 1
b 1
b 0
c 0
c 1
d 0
d 0

If the numbers for two identical letters are also identical then I  
want to

remove either the first or the
second row of that letter. If for a letter the numbers are 1 and 0 I  
want to

remove the row with the 0.



?duplicated


That means if the code works I would and up with the following
matrix/data.frame

Letter Number
a 1
b 1
c 1
d 1


But with no 1's associated with 'd' this does not make sense.




Many thanks,
Syrvn



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.


Re: [R] Remove specific rows in a matrix/data.frame

2011-10-13 Thread Eik Vettorazzi
Hi Syrvn,
how about this
dtf-read.table(textConnection(Letter Number
a 1
a 1
b 1
b 0
c 0
c 1
d 0
d 0),header=T)
aggregate(Number~Letter,data=dtf,max)

cheers.

Am 13.10.2011 18:42, schrieb syrvn:
 Hi,
 
 
 imagine the following matrix/data.frame
 
 Letter Number
 a 1
 a 1
 b 1
 b 0
 c 0
 c 1
 d 0
 d 0 
 
 If the numbers for two identical letters are also identical then I want to
 remove either the first or the
 second row of that letter. If for a letter the numbers are 1 and 0 I want to
 remove the row with the 0.
 
 That means if the code works I would and up with the following
 matrix/data.frame
 
 Letter Number
 a 1
 b 1
 c 1
 d 1
 
 
 Many thanks,
 Syrvn
 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902149.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.


-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und 
Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. 
Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 

__
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] Remove specific rows in a matrix/data.frame

2011-10-13 Thread jim holtman
try this:

 x - read.table(textConnection(Letter Number
+ a 1
+ a 1
+ b 1
+ b 0
+ c 0
+ c 1
+ d 0
+ d 0), as.is = TRUE, header = TRUE)
 closeAllConnections()
 # following assumes that there are pairs of numbers
 result - do.call(rbind, lapply(split(x, x$Letter), function(.pair){
+ if (all(.pair$Number[1L] == .pair$Number)) return(.pair[1L, ])
+ is1 - which(.pair$Number == 1)
+ if (length(is1) == 1) return(.pair[is1, ])
+ else return(NULL)
+ }))

 result
  Letter Number
a  a  1
b  b  1
c  c  1
d  d  0



On Thu, Oct 13, 2011 at 12:42 PM, syrvn ment...@gmx.net wrote:
 Hi,


 imagine the following matrix/data.frame

 Letter Number
 a 1
 a 1
 b 1
 b 0
 c 0
 c 1
 d 0
 d 0

 If the numbers for two identical letters are also identical then I want to
 remove either the first or the
 second row of that letter. If for a letter the numbers are 1 and 0 I want to
 remove the row with the 0.

 That means if the code works I would and up with the following
 matrix/data.frame

 Letter Number
 a 1
 b 1
 c 1
 d 1


 Many thanks,
 Syrvn


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902149.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?

__
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] Remove specific rows in a matrix/data.frame

2011-10-13 Thread Vining, Kelly
Why would you end up with d1 in your output if you don't have a d1 in your 
original data frame? Are you saying that, when both letters have a zero after 
them, you want to replace one of them with a 1?



-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of syrvn
Sent: Thursday, October 13, 2011 9:43 AM
To: r-help@r-project.org
Subject: [R] Remove specific rows in a matrix/data.frame

Hi,


imagine the following matrix/data.frame

Letter Number
a 1
a 1
b 1
b 0
c 0
c 1
d 0
d 0 

If the numbers for two identical letters are also identical then I want to 
remove either the first or the second row of that letter. If for a letter the 
numbers are 1 and 0 I want to remove the row with the 0.

That means if the code works I would and up with the following matrix/data.frame

Letter Number
a 1
b 1
c 1
d 1


Many thanks,
Syrvn


--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902149.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.

__
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] Remove specific rows in a matrix/data.frame

2011-10-13 Thread Jean V Adams
syrvn wrote on 10/13/2011 11:42:44 AM:
 
 Hi,
 
 
 imagine the following matrix/data.frame
 
 Letter Number
 a 1
 a 1
 b 1
 b 0
 c 0
 c 1
 d 0
 d 0 
 
 If the numbers for two identical letters are also identical then I want 
to
 remove either the first or the
 second row of that letter. If for a letter the numbers are 1 and 0 I 
want to
 remove the row with the 0.
 
 That means if the code works I would and up with the following
 matrix/data.frame
 
 Letter Number
 a 1
 b 1
 c 1
 d 1
 
 
 Many thanks,
 Syrvn


I assume that you made a typo when showing the results you want. Following 
your explanation, this is what I get:

df - data.frame(Letter=letters[rep(1:4, rep(2, 4))], Number=c(1, 1, 1, 0, 
0, 1, 0, 0))

# first, get rid of duplicates
df2 - df[!duplicated(df), ]

# then, get rid of 0s from letters that have both 0 and 1
both01 - tapply(df2$Number, df2$Letter, function(x) 
any(as.integer(x)==0)  any(as.integer(x)==1))
delete - df2$Letter %in% names(both01)[both01]  
as.integer(df2$Number)==0
df3 - df2[!delete, ]

# result
df3

  Letter Number
1  a  1
3  b  1
6  c  1
7  d  0


Jean
[[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.


Re: [R] Remove specific rows in a matrix/data.frame

2011-10-13 Thread syrvn
Thanks for your answers! Will check them now :)

Yes, sorry, I was wrong.

Letter Number
d 0
d 0

should be:

Letter Number
d 0

after applying the algorithm!

--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902216.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] Remove specific rows in a matrix/data.frame

2011-10-13 Thread syrvn
Hello again,


dtf-read.table(textConnection(Letter Test Number 
a b 1 
a b 1 
b b 1 
b b 0 
c b 0 
c b 1 
d b 0 
d b 0),header=T) 
aggregate(Number ~ Letter,data=dtf,max)

how can I adjust this solution that the results also includes Test?

I tried:

aggregate(Number ~ Letter,data=dtf,max,by=list(Letter, Test, Number))

But it breaks with the following error message:

Error in aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...) : 
  arguments must have same length





--
View this message in context: 
http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902286.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] Remove specific rows in a matrix/data.frame

2011-10-13 Thread Eik Vettorazzi
Hi,
just put it in the formula:

aggregate(Number ~ Letter+Test,data=dtf,max)

cheers

Am 13.10.2011 19:30, schrieb syrvn:
 Hello again,
 
   
 dtf-read.table(textConnection(Letter Test Number 
   a b 1 
   a b 1 
   b b 1 
   b b 0 
   c b 0 
   c b 1 
   d b 0 
   d b 0),header=T) 
 aggregate(Number ~ Letter,data=dtf,max)
 
 how can I adjust this solution that the results also includes Test?
 
 I tried:
 
 aggregate(Number ~ Letter,data=dtf,max,by=list(Letter, Test, Number))
 
 But it breaks with the following error message:
 
 Error in aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...) : 
   arguments must have same length
 
 
 
 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Remove-specific-rows-in-a-matrix-data-frame-tp3902149p3902286.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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

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