Hello, I have a dataframe 1 2006-11 NaN 2 2006-12 NaN 3 2006-10 0.1577647 4 2006-11 NaN 5 2006-12 NaN 6 2007-01 NaN 7 2007-02 NaN 8 2007-03 0.2956429 9 2007-01 NaN 10 2007-02 NaN I need to trim first and last NaN rows Result - 1 2006-10 0.1577647 2 2006-11 NaN 3 2006-12 NaN 4 2007-01 NaN 5 2007-02 NaN 6 2007-03 0.2956429 Thanks.
Hi, May be this helps: dat1<-read.table(text=" 1 2006-11????? NaN 2 2006-12????? NaN 3 2006-10 0.1577647 4 2006-11????? NaN 5 2006-12????? NaN 6 2007-01????? NaN 7 2007-02????? NaN 8 2007-03 0.2956429 9 2007-01????? NaN 10 2007-02????? NaN ",sep="",header=FALSE,stringsAsFactors=FALSE) res<-dat1[seq(which(!is.na(dat1$V3))[1],which(!is.na(dat1$V3))[2],by=1),] ?res #? V1????? V2??????? V3 #3? 3 2006-10 0.1577647 #4? 4 2006-11?????? NaN #5? 5 2006-12?????? NaN #6? 6 2007-01?????? NaN #7? 7 2007-02?????? NaN #8? 8 2007-03 0.2956429 A.K. ----- Original Message ----- From: Vasilchenko Aleksander <vasilchenko.a.p at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, December 5, 2012 5:46 AM Subject: [R] Trim Hello, I have a dataframe 1 2006-11? ? ? NaN 2 2006-12? ? ? NaN 3 2006-10 0.1577647 4 2006-11? ? ? NaN 5 2006-12? ? ? NaN 6 2007-01? ? ? NaN 7 2007-02? ? ? NaN 8 2007-03 0.2956429 9 2007-01? ? ? NaN 10 2007-02? ? ? NaN I need to trim first and last NaN rows Result - 1 2006-10 0.1577647 2 2006-11? ? ? NaN 3 2006-12? ? ? NaN 4 2007-01? ? ? NaN 5 2007-02? ? ? NaN 6 2007-03 0.2956429 Thanks. ______________________________________________ R-help at 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.
Hello, Try the following. dat <- structure(list(V1 = structure(c(2L, 3L, 1L, 2L, 3L, 4L, 5L, 6L, 4L, 5L), .Label = c("2006-10", "2006-11", "2006-12", "2007-01", "2007-02", "2007-03"), class = "factor"), V2 = c(NaN, NaN, 0.1577647, NaN, NaN, NaN, NaN, 0.2956429, NaN, NaN)), .Names = c("V1", "V2" ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")) idx <- cumsum(!is.nan(dat$V2)) * rev(cumsum(rev(!is.nan(dat$V2)))) != 0 dat[idx, ] Hope this helps, Rui Barradas Em 05-12-2012 10:46, Vasilchenko Aleksander escreveu:> Hello, > I have a dataframe > > 1 2006-11 NaN > 2 2006-12 NaN > 3 2006-10 0.1577647 > 4 2006-11 NaN > 5 2006-12 NaN > 6 2007-01 NaN > 7 2007-02 NaN > 8 2007-03 0.2956429 > 9 2007-01 NaN > 10 2007-02 NaN > I need to trim first and last NaN rows > Result - > 1 2006-10 0.1577647 > 2 2006-11 NaN > 3 2006-12 NaN > 4 2007-01 NaN > 5 2007-02 NaN > 6 2007-03 0.2956429 > Thanks. > > ______________________________________________ > R-help at 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.
Hi, I guess with only one rev() should also work: ?idx<-cumsum(!is.na(dat$V2))*rev(cumsum(!is.na(dat$V2)))!=0 ?dat[idx,] #?????? V1??????? V2 #3 2006-10 0.1577647 #4 2006-11?????? NaN #5 2006-12?????? NaN #6 2007-01?????? NaN #7 2007-02?????? NaN #8 2007-03 0.2956429 A.K. ----- Original Message ----- From: Rui Barradas <ruipbarradas at sapo.pt> To: Vasilchenko Aleksander <vasilchenko.a.p at gmail.com> Cc: r-help at r-project.org Sent: Wednesday, December 5, 2012 7:59 AM Subject: Re: [R] Trim Hello, Try the following. dat <- structure(list(V1 = structure(c(2L, 3L, 1L, 2L, 3L, 4L, 5L, 6L, 4L, 5L), .Label = c("2006-10", "2006-11", "2006-12", "2007-01", "2007-02", "2007-03"), class = "factor"), V2 = c(NaN, NaN, 0.1577647, NaN, NaN, NaN, NaN, 0.2956429, NaN, NaN)), .Names = c("V1", "V2" ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")) idx <- cumsum(!is.nan(dat$V2)) * rev(cumsum(rev(!is.nan(dat$V2)))) != 0 dat[idx, ] Hope this helps, Rui Barradas Em 05-12-2012 10:46, Vasilchenko Aleksander escreveu:> Hello, > I have a dataframe > > 1 2006-11? ? ? NaN > 2 2006-12? ? ? NaN > 3 2006-10 0.1577647 > 4 2006-11? ? ? NaN > 5 2006-12? ? ? NaN > 6 2007-01? ? ? NaN > 7 2007-02? ? ? NaN > 8 2007-03 0.2956429 > 9 2007-01? ? ? NaN > 10 2007-02? ? ? NaN > I need to trim first and last NaN rows > Result - > 1 2006-10 0.1577647 > 2 2006-11? ? ? NaN > 3 2006-12? ? ? NaN > 4 2007-01? ? ? NaN > 5 2007-02? ? ? NaN > 6 2007-03 0.2956429 > Thanks. > > ______________________________________________ > R-help at 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 at 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.
On 05-12-2012, at 11:46, Vasilchenko Aleksander wrote:> Hello, > I have a dataframe > > 1 2006-11 NaN > 2 2006-12 NaN > 3 2006-10 0.1577647 > 4 2006-11 NaN > 5 2006-12 NaN > 6 2007-01 NaN > 7 2007-02 NaN > 8 2007-03 0.2956429 > 9 2007-01 NaN > 10 2007-02 NaN > I need to trim first and last NaN rows > Result - > 1 2006-10 0.1577647 > 2 2006-11 NaN > 3 2006-12 NaN > 4 2007-01 NaN > 5 2007-02 NaN > 6 2007-03 0.2956429dat <- + structure(list(V1 = structure(c(2L, 3L, 1L, 2L, 3L, 4L, 5L, 6L, + 4L, 5L), .Label = c("2006-10", "2006-11", "2006-12", "2007-01", + "2007-02", "2007-03"), class = "factor"), V2 = c(NaN, NaN, 0.1577647, + NaN, NaN, NaN, NaN, 0.2956429, NaN, NaN)), .Names = c("V1", "V2" + ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", + "6", "7", "8", "9", "10")) library(zoo) na.trim(dat) Berend