Loop for matching lists of names and populating a new variable according to the gender of names

Suppose I have the following lists, where "names" is a complete list of names (for example, in a class):

names<-as.matrix(c("Paul", "Tyler", "Roberta", "Greg", "Tiffany"))

Suppose I have a secondary list of names that includes only female names:

female_names<-as.matrix(c("Roberta", "Tiffany", "Michelle", "Ashley"))

I am trying to create another "woman" variable that takes the value 1 if the element in the "names" matches one of the "female names" in the second list at the top.

women<-as.matrix(rep(0, 5))

for(i in 1:nrow(names)){
  for(j in 1:nrow(female_names)){
    if(names[i,1]==female_names[j,1]){women[i]<-1}
  }
}

However, when I summarize a new woman with variables, all values ​​are 0, which should not be.

summary(women)

, 1 . ?

+3
2

%in%:

> names[, 1] %in% female_names[, 1]
[1] FALSE FALSE  TRUE FALSE  TRUE

as.numeric, 1 0 TRUE FALSE:

> as.numeric(names[, 1] %in% female_names[, 1])
[1] 0 0 1 0 1
+4

, , , .

dev qdap (name2sex), :

names<-c("Paul", "Tyler", "Roberta", "Greg", "Tiffany")
name2sex(names)

## > name2sex(names)
## [1] M M F M F

## OR....

name2sex(names, USE.NAMES=TRUE)

## > name2sex(names, USE.NAMES=TRUE)
##    Paul   Tyler Roberta    Greg Tiffany 
##       M       M       F       M       F 

:

## 2 - as.numeric(name2sex(names))

## > 2 -as.numeric(name2sex(names))
## [1] 0 0 1 0 1
+1

All Articles