|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
Delete Block of RowsI am trying to delete a section of rows from a data frame (based on no condition). Lets say my data frame has 200 rows and I want to delete rows 1 through 25. How would I do this?
I know x[ -1, ] deletes the first row (or any desired row). I tried using different variations of this, like x[ -c(1:25), ] but that didn't work. I also tried writting a few functions, for example: deleteRows <- function( data, s, e, ) { for( i in s:e ) data[ -i, ] } deleteRows( ds, 1, 25 ) But that didn't work either, it only deleted row 25 (i'm new to writing functions). Any thoughts on how to solve my problem would be appreciated. |
|
|
Re: Delete Block of RowsHi there,
I don´t know if you are trying to solve the delete job or to test how functions work. If you really want to delete lines from a data.frame, try something like this. rowcount<-1:100 x<-runif(100) y<-runif(100) df<-data.frame(cbind(rowcount,x,y)) df.subset<-subset(df, !(rownames(df) %in% 1:25)) # ! (a condition) is the negation of the condition # so the output will be those lines that are not on interval 1:25 By the way, it is not a good idea to use "data" as a input argument on a function because "data" is a pre-defined function. Kind regards, miltinho On 6/15/08, nmarti <nate318i@...> wrote: > > > I am trying to delete a section of rows from a data frame (based on no > condition). Lets say my data frame has 200 rows and I want to delete rows > 1 > through 25. How would I do this? > > I know x[ -1, ] deletes the first row (or any desired row). I tried using > different variations of this, like x[ -c(1:25), ] but that didn't work. I > also tried writting a few functions, for example: > > deleteRows <- function( data, s, e, ) { > for( i in s:e ) > data[ -i, ] > } > deleteRows( ds, 1, 25 ) > > But that didn't work either, it only deleted row 25 (i'm new to writing > functions). > Any thoughts on how to solve my problem would be appreciated. > -- > View this message in context: > http://www.nabble.com/Delete-Block-of-Rows-tp17849775p17849775.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@... 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@... 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: Delete Block of Rowsnmarti wrote:
> I am trying to delete a section of rows from a data frame (based on no > condition). Lets say my data frame has 200 rows and I want to delete rows 1 > through 25. How would I do this? > > I know x[ -1, ] deletes the first row (or any desired row). I tried using > different variations of this, like x[ -c(1:25), ] but that didn't work. what do you mean by 'didnt work'? when you use negative indices, rows (or columns) from the input are skipped in the output. but to have a side effect of actually changing the data frame, you need an assignment. to modify your data frame, you need: x = x[-c(<your spec>),] while x[-c(<your spec>),] will (should) return a df with rows skipped, but x won't be changed. > I > also tried writting a few functions, for example: > > deleteRows <- function( data, s, e, ) { > for( i in s:e ) > data[ -i, ] > } > deleteRows( ds, 1, 25 ) > > But that didn't work either, it only deleted row 25 df with all rows of ds except row 25 (in a bad style, i'd say). > (i'm new to writing > functions). > consider reading the docs. note that an implementation like deleteRows = function(df, s, e) (df = df[-(s:e),,drop=FALSE]) won't work, because in r data frames are passed as arguments to functions by value (you are welcome to argue that this is insane). vQ ______________________________________________ R-help@... 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: Delete Block of RowsI believe that x <- x[-(1:25),] should work.
--- On Sun, 15/6/08, nmarti <nate318i@...> wrote: > From: nmarti <nate318i@...> > Subject: [R] Delete Block of Rows > To: r-help@... > Received: Sunday, 15 June, 2008, 11:02 PM > I am trying to delete a section of rows from a data frame > (based on no > condition). Lets say my data frame has 200 rows and I want > to delete rows 1 > through 25. How would I do this? > > I know x[ -1, ] deletes the first row (or any desired row). > I tried using > different variations of this, like x[ -c(1:25), ] but that > didn't work. I > also tried writting a few functions, for example: > > deleteRows <- function( data, s, e, ) { > for( i in s:e ) > data[ -i, ] > } > deleteRows( ds, 1, 25 ) > > But that didn't work either, it only deleted row 25 > (i'm new to writing > functions). > Any thoughts on how to solve my problem would be > appreciated. > -- > View this message in context: > http://www.nabble.com/Delete-Block-of-Rows-tp17849775p17849775.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@... 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@... 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: Delete Block of RowsThanks for all the replies.
My data frame is actually 4000 rows and 10 columns. This line, df <- subset( df, !( rownames(df) %in% 1:25 ) ) Did not work for me. Is there a certain library I need to load for this to work (I'm assuming there isn't)? I didn't get an error message, warning, or anything; it just didn't work. These two lines did work, df = df[ -c(1:25), ] Or, df = df[ 26:200, ] I'm not sure why they didn't work the first time I tried them. Once again, thanks for the help!
|
|
|
Re: Delete Block of RowsHi there,
The subset function is available on your Base R. So you need not to load any package. May be it is not working because I suppose that your "rownames" is a sequential one (1...4000). Case it is not true, the command will not work. Anyway, I think that those line that are working find is simple than you use %in%... etc. Good luck, miltinho Brazil On 6/16/08, nmarti <nate318i@...> wrote: > > > Thanks for all the replies. > My data frame is actually 4000 rows and 10 columns. > > This line, > df <- subset( df, !( rownames(df) %in% 1:25 ) ) Did not work for me. Is > there a certain library I need to load for this to work (I'm assuming there > isn't)? I didn't get an error message, warning, or anything; it just > didn't > work. > > These two lines did work, > df = df[ -c(1:25), ] Or, > df = df[ 26:200, ] I'm not sure why they didn't work the first time I > tried them. > > Once again, thanks for the help! > > > > milton ruser wrote: > > > > Hi there, > > > > I don´t know if you are trying to solve the delete job or to test how > > functions work. > > > > If you really want to delete lines from a data.frame, try something like > > this. > > > > > > rowcount<-1:100 > > x<-runif(100) > > y<-runif(100) > > > > df<-data.frame(cbind(rowcount,x,y)) > > > > df.subset<-subset(df, !(rownames(df) %in% 1:25)) > > > > # ! (a condition) is the negation of the condition > > > > # so the output will be those lines that are not on interval 1:25 > > > > > > > > By the way, it is not a good idea to use "data" as a input argument on > > a function because "data" is a pre-defined function. > > Kind regards, > > > > miltinho > > > > > > > > On 6/15/08, nmarti <nate318i@...> wrote: > >> > >> > >> I am trying to delete a section of rows from a data frame (based on no > >> condition). Lets say my data frame has 200 rows and I want to delete > >> rows > >> 1 > >> through 25. How would I do this? > >> > >> I know x[ -1, ] deletes the first row (or any desired row). I tried > >> using > >> different variations of this, like x[ -c(1:25), ] but that didn't work. > >> I > >> also tried writting a few functions, for example: > >> > >> deleteRows <- function( data, s, e, ) { > >> for( i in s:e ) > >> data[ -i, ] > >> } > >> deleteRows( ds, 1, 25 ) > >> > >> But that didn't work either, it only deleted row 25 (i'm new to writing > >> functions). > >> Any thoughts on how to solve my problem would be appreciated. > >> -- > >> View this message in context: > >> http://www.nabble.com/Delete-Block-of-Rows-tp17849775p17849775.html > >> Sent from the R help mailing list archive at Nabble.com. > >> > >> ______________________________________________ > >> R-help@... 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. > >> > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > R-help@... 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. > > > > > > -- > View this message in context: > http://www.nabble.com/Delete-Block-of-Rows-tp17849775p17865279.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@... 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@... 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. |
| Free embeddable forum powered by Nabble | Forum Help |