Remove a complete row as per the Range in a Matrix

 Classic List Threaded
6 messages
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Remove a complete row as per the Range in a Matrix

 Hi, Here i have a Matrix MyMatrix <- Name            Age ---------           ------- ANTONY        27 IMRAN           30 RAJ                   22 NAHAS           32 GEO                 42 and here i have an array with Minimum and Maximum values. MinMaxArray  <- data.frame(MIN = 25,MAX=35) MIN     MAX ------     -------- 25           35 ------------------------------------------------------ Now what is, i need to get a matrix by removing the rows which is NOT coming the "Age"-column in between the MIN and MAX value.  Is it possible to avoid "for-loop", bcz its a huge matrix. so, final matrix looks like this, Name            Age ---------           ------- ANTONY        27 IMRAN           30 NAHAS           32 - Thanks in advance, Antony.
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: Remove a complete row as per the Range in a Matrix

 Hello, Please learn how to use dput(), it's not your first post. And try the following. myMatrix <- data.matrix(read.table(text=" Name            Age ANTONY        27 IMRAN           30 RAJ                   22 NAHAS           32 GEO                 42 ", header=TRUE)) MinMaxArray  <- data.frame(MIN = 25,MAX=35) inx <- MinMaxArray[[ "MIN" ]] <= myMatrix[, "Age"] & myMatrix[, "Age"] <= MinMaxArray[[ "MAX" ]] myMatrix[ inx , ] Hope this helps, Rui Barradas Em 31-07-2012 08:58, Rantony escreveu: > Hi, > > Here i have a Matrix > > MyMatrix <- > > Name            Age > ---------           ------- > ANTONY        27 > IMRAN           30 > RAJ                   22 > NAHAS           32 > GEO                 42 > > and here i have an array with Minimum and Maximum values. > MinMaxArray  <- data.frame(MIN = 25,MAX=35) > MIN     MAX > ------     -------- > 25           35 > ------------------------------------------------------ > Now what is, i need to get a matrix by removing the rows which is NOT coming > the "Age"-column in between the MIN and MAX value.  Is it possible to avoid > "for-loop", bcz its a huge matrix. > > so, final matrix looks like this, > > Name            Age > ---------           ------- > ANTONY        27 > IMRAN           30 > NAHAS           32 > > > - Thanks in advance, > Antony. > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Remove-a-complete-row-as-per-the-Range-in-a-Matrix-tp4638508.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: Remove a complete row as per the Range in a Matrix

 In reply to this post by R_Antony Hi, Try this: myMatrix<-read.table(text=" Name            Age ANTONY        27 IMRAN          30 RAJ                  22 NAHAS          32 GEO                42 ",sep="",header=TRUE) MinMaxArray  <- data.frame(MIN = 25,MAX=35) myMatrix[myMatrix\$Age<=MinMaxArray\$MAX & myMatrix\$Age>=MinMaxArray\$MIN,]   #  Name Age #1 ANTONY  27 #2  IMRAN  30 #4  NAHAS  32 #or subset(myMatrix,myMatrix\$Age<=MinMaxArray\$MAX & myMatrix\$Age>=MinMaxArray\$MIN) #    Name Age #1 ANTONY  27 #2  IMRAN  30 #4  NAHAS  32 A.K. ----- Original Message ----- From: Rantony <[hidden email]> To: [hidden email] Cc: Sent: Tuesday, July 31, 2012 3:58 AM Subject: [R] Remove a complete row as per the Range in a Matrix Hi, Here i have a Matrix MyMatrix <- Name            Age ---------           ------- ANTONY        27 IMRAN           30 RAJ                   22 NAHAS           32 GEO                 42 and here i have an array with Minimum and Maximum values. MinMaxArray  <- data.frame(MIN = 25,MAX=35) MIN     MAX ------     -------- 25           35 ------------------------------------------------------ Now what is, i need to get a matrix by removing the rows which is NOT coming the "Age"-column in between the MIN and MAX value.  Is it possible to avoid "for-loop", bcz its a huge matrix. so, final matrix looks like this, Name            Age ---------           ------- ANTONY        27 IMRAN           30 NAHAS           32 - Thanks in advance, Antony. -- View this message in context: http://r.789695.n4.nabble.com/Remove-a-complete-row-as-per-the-Range-in-a-Matrix-tp4638508.htmlSent from the R help mailing list archive at Nabble.com. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: Remove a complete row as per the Range in a Matrix

 This post has NOT been accepted by the mailing list yet. In reply to this post by R_Antony Hi, Try this: myMatrix<-read.table(text=" Name            Age ANTONY        27 IMRAN          30 RAJ                  22 NAHAS          32 GEO                42 ",sep="",header=TRUE) MinMaxArray  <- data.frame(MIN = 25,MAX=35) myMatrix[myMatrix\$Age<=MinMaxArray\$MAX & myMatrix\$Age>=MinMaxArray\$MIN,]   #  Name Age #1 ANTONY  27 #2  IMRAN  30 #4  NAHAS  32 #or subset(myMatrix,myMatrix\$Age<=MinMaxArray\$MAX & myMatrix\$Age>=MinMaxArray\$MIN) #    Name Age #1 ANTONY  27 #2  IMRAN  30 #4  NAHAS  32 A.K.
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: Remove a complete row as per the Range in a Matrix

 In reply to this post by Rui Barradas On Jul 31, 2012, at 1:31 AM, Rui Barradas wrote: > Hello, > > Please learn how to use dput(), it's not your first post. > And try the following. > > > myMatrix <- data.matrix(read.table(text=" I think the data.matrix transformation is a bad idea. It forces the   numeric values to be character and the collation sequence is driven   off the first letter, so "19" is less than "9", and "1010" is less   than "2" > Name            Age > ANTONY        27 > IMRAN           30 > RAJ                   22 > NAHAS           32 > GEO                 42 > ", header=TRUE)) > Why not just: myDF <- read.table(text="Name            Age ANTONY        27 IMRAN           30 RAJ                   22 NAHAS           32 GEO                 42 ", header=TRUE) MinMax <- c(MIN = 25,MAX=35) # using a named vector rather than a two column dataframe  > myDF[ myDF\$Age > MinMax["MIN"] & myDF\$Age < MinMax["MAX"] , ]      Name Age 1 ANTONY  27 2  IMRAN  30 4  NAHAS  32 -- David. > MinMaxArray  <- data.frame(MIN = 25,MAX=35) > > inx <- MinMaxArray[[ "MIN" ]] <= myMatrix[, "Age"] & myMatrix[,   > "Age"] <= MinMaxArray[[ "MAX" ]] > myMatrix[ inx , ] > > Hope this helps, > > Rui Barradas > Em 31-07-2012 08:58, Rantony escreveu: >> Hi, >> >> Here i have a Matrix >> >> MyMatrix <- >> >> Name            Age >> ---------           ------- >> ANTONY        27 >> IMRAN           30 >> RAJ                   22 >> NAHAS           32 >> GEO                 42 >> >> and here i have an array with Minimum and Maximum values. >> MinMaxArray  <- data.frame(MIN = 25,MAX=35) >> MIN     MAX >> ------     -------- >> 25           35 >> ------------------------------------------------------ >> Now what is, i need to get a matrix by removing the rows which is   >> NOT coming >> the "Age"-column in between the MIN and MAX value.  Is it possible   >> to avoid >> "for-loop", bcz its a huge matrix. >> >> so, final matrix looks like this, >> >> Name            Age >> ---------           ------- >> ANTONY        27 >> IMRAN           30 >> NAHAS           32 >> >> >> - Thanks in advance, >> Antony. >> > -- David Winsemius, MD Alameda, CA ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: Remove a complete row as per the Range in a Matrix

 This post has NOT been accepted by the mailing list yet. In reply to this post by R_Antony Hi, You could also use ifelse. myDF <- read.table(text="Name            Age ANTONY        27 IMRAN           30 RAJ                   22 NAHAS           32 GEO                 42 ", sep="",header=TRUE) MinMax<-data.frame(Age=c(25,35))  myDF[ifelse(myDF\$Age < max(MinMax\$Age) & myDF\$Age > min(MinMax\$Age),TRUE,FALSE),]     Name Age 1 ANTONY  27 2  IMRAN  30 4  NAHAS  32 A.K.
Loading...