remove

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

remove

Val-17
Hi all,
I have  a date  issue and would appreciate any help.

I am reading a field data and  n one of the columns I am expecting a
date but has  non date  values  such as  character and  empty. space.
Here is a sample of my data.

KL <- read.table(header=TRUE, text='ID date
711 Dead
712 Uknown
713 20-11-08
714 11-28-07
301
302 09-02-02
303 09-21-02',stringsAsFactors = FALSE, fill =T)

str(KL)
data.frame': 7 obs. of  2 variables:
 $ ID  : int  711 712 713 714 301 302 303
 $ date: chr  "Dead" "Uknown" "20-11-08" "11-28-07" .

I wanted to convert the date column as follows.
if (max(unique(nchar(as.character(KL$date))))==10) {
  KL$date <- as.Date(KL$date,"%m/%d/%Y")
}
but not working.


How  could I to remove the corresponding entire row. that do not have
a date format and do the operation?
thank you in advance

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: remove

Jeff Newmiller
You are using a slash in your format string to separate sub-fields but your data uses a dash.
--
Sent from my phone. Please excuse my brevity.

On June 10, 2017 8:18:37 PM PDT, Val <[hidden email]> wrote:

>Hi all,
>I have  a date  issue and would appreciate any help.
>
>I am reading a field data and  n one of the columns I am expecting a
>date but has  non date  values  such as  character and  empty. space.
>Here is a sample of my data.
>
>KL <- read.table(header=TRUE, text='ID date
>711 Dead
>712 Uknown
>713 20-11-08
>714 11-28-07
>301
>302 09-02-02
>303 09-21-02',stringsAsFactors = FALSE, fill =T)
>
>str(KL)
>data.frame': 7 obs. of  2 variables:
> $ ID  : int  711 712 713 714 301 302 303
> $ date: chr  "Dead" "Uknown" "20-11-08" "11-28-07" .
>
>I wanted to convert the date column as follows.
>if (max(unique(nchar(as.character(KL$date))))==10) {
>  KL$date <- as.Date(KL$date,"%m/%d/%Y")
>}
>but not working.
>
>
>How  could I to remove the corresponding entire row. that do not have
>a date format and do the operation?
>thank you in advance
>
>______________________________________________
>[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.

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: remove

Bert Gunter-2
Also ?ifelse  rather than if()  I think.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Jun 10, 2017 at 10:17 PM, Jeff Newmiller
<[hidden email]> wrote:

> You are using a slash in your format string to separate sub-fields but your data uses a dash.
> --
> Sent from my phone. Please excuse my brevity.
>
> On June 10, 2017 8:18:37 PM PDT, Val <[hidden email]> wrote:
>>Hi all,
>>I have  a date  issue and would appreciate any help.
>>
>>I am reading a field data and  n one of the columns I am expecting a
>>date but has  non date  values  such as  character and  empty. space.
>>Here is a sample of my data.
>>
>>KL <- read.table(header=TRUE, text='ID date
>>711 Dead
>>712 Uknown
>>713 20-11-08
>>714 11-28-07
>>301
>>302 09-02-02
>>303 09-21-02',stringsAsFactors = FALSE, fill =T)
>>
>>str(KL)
>>data.frame': 7 obs. of  2 variables:
>> $ ID  : int  711 712 713 714 301 302 303
>> $ date: chr  "Dead" "Uknown" "20-11-08" "11-28-07" .
>>
>>I wanted to convert the date column as follows.
>>if (max(unique(nchar(as.character(KL$date))))==10) {
>>  KL$date <- as.Date(KL$date,"%m/%d/%Y")
>>}
>>but not working.
>>
>>
>>How  could I to remove the corresponding entire row. that do not have
>>a date format and do the operation?
>>thank you in advance
>>
>>______________________________________________
>>[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.
>
> ______________________________________________
> [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.

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: remove

Jeff Newmiller
In reply to this post by Jeff Newmiller
The usual way I filter is:

KL$Dt <- as.Date( KL$date, format='%d-%m-%y' )
KL2 <- KL[ !is.na( KL$Dt ), ]

--
Sent from my phone. Please excuse my brevity.

On June 10, 2017 10:17:52 PM PDT, Jeff Newmiller <[hidden email]> wrote:
>You are using a slash in your format string to separate sub-fields but
>your data uses a dash.

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