R check last letter of string as condition

15 views
Skip to first unread message

DrunkenPhD

unread,
Jul 29, 2015, 3:40:36 PM7/29/15
to manipulatr
Dear all,
I have a data frame with two columns:

Name   Sex

Sara      F
Jane      F
Joh    M


I need to check all names and if I find names ending with letter A or E column SEX has to get a F value.
Otherwise values of SEX column has to be M.
Can you please help with this?
Best Regards

jim holtman

unread,
Jul 29, 2015, 3:49:54 PM7/29/15
to DrunkenPhD, manipulatr
df$Sex <- ifelse(grepl("(a|e)$", df$Name), "F", "M")


Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at http://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

Doug Mitarotonda

unread,
Jul 29, 2015, 3:50:49 PM7/29/15
to DrunkenPhD, manipulatr
This seems more like a regular expression question than anything. I would just do something like, 

# dft is your data frame
is_female <- stringr::str_detect(dft$Name, “[a|e]$”)
dft$Sex <- “M”
dft$Sex[is_female] <- “F"

Endri Raco

unread,
Jul 29, 2015, 3:55:27 PM7/29/15
to Doug Mitarotonda, manipulatr
Thank you guys.

Unfortunately both solutions return to M all values of SEX column so still not success:(

Regards
--
 Endri Raço PhD
_________________________________________________
Polytechnic University of Tirana
Faculty of Mathematical Engineering and Physics Engineering
Department of
Mathematical Engineering
Address:  Sheshi Nene Tereza, Nr. 4, Tirana - ALBANIA
Mobile: ++ 355 682061988

DrunkenPhD

unread,
Jul 29, 2015, 3:56:35 PM7/29/15
to manipulatr, end...@gmail.com
Still not working

Both solutions fill with M all Sex column

Regards


DrunkenPhD

unread,
Jul 29, 2015, 4:00:33 PM7/29/15
to manipulatr, end...@gmail.com
OK my error because I have uppercase.
    Both solutions working.
    Thank you Jim and Dough excellent work guys
     Best Regards 

Doug Mitarotonda

unread,
Jul 29, 2015, 4:01:16 PM7/29/15
to DrunkenPhD, manipulatr
Then you will need to provide an MWE to show us what is going on (maybe your strings are factors and that is an issue?). Both solutions work for me.

dft <- data.frame(Name = c("Sara", "Jane", "John"), stringsAsFactors = FALSE)
is_female <- str_detect(dft$Name, "[a|e]$”)
dft$Sex <- “M"
dft$Sex[is_female] <- “F"
  Name Sex
1 Sara   F
2 Jane   F
3 John   M

# or

dft <- data.frame(Name = c("Sara", "Jane", "John"), stringsAsFactors = FALSE)
dft$Sex <- ifelse(grepl("(a|e)$", dft$Name), "F", "M")
dft
  Name Sex
1 Sara   F
2 Jane   F
3 John   M


Endri Raco

unread,
Jul 29, 2015, 4:03:37 PM7/29/15
to Doug Mitarotonda, manipulatr
You are totally right, it was my stupid mistake of uppercase.
Everything is working fine, you guys are great
Thanks for the help
Regards
Reply all
Reply to author
Forward
0 new messages