[R] Searching for the first two characters of a string

David Winsemius dwinsemius at comcast.net
Mon Nov 21 16:39:12 CET 2011


On Nov 21, 2011, at 10:00 AM, aclinard wrote:

> Remko,
>
> Thank you for your reply. When I try your code, I'm getting the  
> following
> error:
>
> acts10 <-read.table('RailAccident10.txt',sep=',',header=T)
>> acts11 <- acts10[acts10$ACCDMG > 10^5,]
>> h.test <- substr(acts11$CAUSE,1,1)
>> h.data <- acts11[h.test == 'H']

MIssing comma about here ........^

> Error in `[.data.frame`(acts11, h.test == "H") :
>  undefined columns selected

The error message is telling you that you tried to select a colum nad  
you should have thought to yourself:
" a "column"?
    I wanted a row.
    So what have I done that made the "[" function "think" I wanted a  
column?

(Yes, I know that computers can't think. But they can report what they  
were attempting to do when they "gave up" in desperation about being  
told to do impossible things by their owners. )
>
> To confirm that acts11$CAUSE is there, I did this:
> summary(substr(acts11$CAUSE,1,1)=='H')
>
> And got this:
>
>   Mode   FALSE    TRUE    NA's
> logical     393     156       0
>
> Any idea what I'm doing wrong?
>
> Thanks again, I really appreciate your help.
> On Sun, Nov 20, 2011 at 7:13 PM, Remko Duursma [via R] <
> ml-node+s789695n4090042h18 at n4.nabble.com> wrote:
>
>>
>> dfr <- data.frame(txt= c("abab","ghghg","ththt","dfdfdf"), yvar=1:4)
>>
>> ind <- substr(dfr$txt,1,2)
>>
>> dfr[ind == "ab",]
>>
>>
>>
>> greetings,
>> Remko
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the  
>> discussion
>> below:
>>
>> http://r.789695.n4.nabble.com/Searching-for-the-first-two-characters-of-a-string-tp4089862p4090042.html
>> To unsubscribe from Searching for the first two characters of a  
>> string, click
>> here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4089862&code=YWxhbi5jbGluYXJkQGdtYWlsLmNvbXw0MDg5ODYyfC01NTMwMzEzOTY= 
>> >
>> .
>> NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.InstantMailNamespace&breadcrumbs=instant+emails%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml 
>> >
>>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Searching-for-the-first-two-characters-of-a-string-tp4089862p4091786.html
> Sent from the R help mailing list archive at Nabble.com.
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list