# Cleaning data

3 messages
Open this post in threaded view
|

## Cleaning data

 Hi I want to clean my data frame, based on the age column, whereas i want to delete the rows that the difference between its elements (i+1)-i= integer. i used a <- diff(df\$age) for(i in a){if(is.integer(a) == true){df <- df[-a,] }} but, it doesn’t work, any ideas Thanks in advance Bayan ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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.
Open this post in threaded view
|

## Re: Cleaning data

 Hi Bayan, In your code, 'a' is a vector and is.integer(a) is a logical of length 1 - most likely FALSE if even one element of a is not an integer. (Since R will coerce all the elements of a to the same type.) You need to decide whether something "close enough" to an integer is to be considered an integer - e.g. a distance of 0.000001 = 1e-6.  a <- df\$age df <- df[ c( TRUE, abs( a - round(a,0) )%%1 ) > 1e-6 ), ] I added the 'TRUE' at the beginning to always keep the first row of df. If you prefer to always keep the last row then move the TRUE to the end. HTH, Eric On Tue, Sep 26, 2017 at 12:50 PM, bayan sardini <[hidden email]> wrote: > Hi > > I want to clean my data frame, based on the age column, whereas i want to > delete the rows that the difference between its elements (i+1)-i= integer. > i used > > a <- diff(df\$age) > for(i in a){if(is.integer(a) == true){df <- df[-a,] > }} > > but, it doesn’t work, any ideas > > Thanks in advance > Bayan > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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.
Open this post in threaded view
|