函数导致 R 中的空输出?

Function result in null output in R?

我有像这样的数据集名称

no   names
1    Anu
2    Bhanwarlal
3    Millennium
4    NA
5    Mushkil
6    NA
7    Niraj
8     NA
9    Dharmender
10    S
11    Hocalwire
12    Mansoor

girl
no   name
1    Anu
2    Komal
3    Piyu

boy 
no   name
1    Amit
2    Niraj
3    Dharmendra
4    Monsoor
5    Akash

示例数据集可以创建为:

# set up a vector of names
names <- c('Anu', 'Bhanwarlal', 'Komal', NA, 'Mushkil', NA, 'Niraj', NA, 'Dharmendra', 'S', 'Amit', 'Monsoor')
# set up a vector of female names
girls <- c('Anu', 'Komal', 'Piyu')
# set up a vector of male names
boys <- c('Amit', 'Niraj', 'Dharmendra', 'Monsoor', 'Akash')

我有数据集名称。如果名字是男孩或女孩,则通过在名字数据集中添加一列,将男性或女性分配给该名字。

gender <- function(names,boy,girl){
  for(i in 1:nrow(names)){
     if(names[i,1] %in% girl$name){
       names$gen[i] <- "Female"
     } else if(names[i,1] %in% boy$name){
       names$gen[i]<- "Male"
     } else
       names$gen[i] <- "NA"
   }
}

gen <- gender(names,boy,girl)

输出:

> gen
NULL

我查不出什么是可能的错误。

以下是进行分析的快速方法:

# names
names <- c('Anu', 'Bhanwarlal', 'Komal', NA, 'Mushkil', NA, 'Niraj', NA, 'Dharmendra', 'S', 'Amit', 'Monsoor')
girls <- c('Anu', 'Komal', 'Piyu', 'Anu')
boys <- c('Amit', 'Niraj', 'Dharmendra', 'Monsoor', 'Akash')

# dataframes
name <- tibble::data_frame(names, gen = NA)
girl <- tibble::data_frame(name = girls, gender = rep('Female', length(girls)))
boy <- tibble::data_frame(name = boys, gender = rep('Male', length(boys)))
map_names <- rbind(girl, boy)

# function
gender <- function(names, map_names){
    n <- nrow(names)
    for(i in seq_len(n)){
        k <- which(names[[i, 1]] == map_names$name)
        if (length(k) != 0) {
            if (length(k) > 1) {
                k <- k[1]
            }
            names[[2]][i] <- map_names$gender[k]    
        } else {
            names[[2]][i] <- 'NA'
        }  
    }
    colnames(names) <- c('names', 'gender')
    return(names)
}

#output
gen <- gender(name, map_names)
gen
# A tibble: 12 × 2
        names gender
        <chr>  <chr>
1         Anu Female
2  Bhanwarlal     NA
3       Komal Female
4        <NA>     NA
5     Mushkil     NA
6        <NA>     NA
7       Niraj   Male
8        <NA>     NA
9  Dharmendra   Male
10          S     NA
11       Amit   Male
12    Monsoor   Male

正如 Jovial Mann 在回答中提到的,您的问题在于没有告诉用户定义的函数它应该 return 任何输出。要解决此问题,只需在函数的最后一行添加 names,您应该会看到一些输出。

接下来可以使用'sapply'函数来避免使用for循环

# set up a vector of names
names <- c('Anu', 'Bhanwarlal', 'Komal', NA, 'Mushkil', NA, 'Niraj', NA, 'Dharmendra', 'S', 'Amit', 'Monsoor')
# set up a vector of female names
girls <- c('Anu', 'Komal', 'Piyu')
# set up a vector of male names
boys <- c('Amit', 'Niraj', 'Dharmendra', 'Monsoor', 'Akash')

# create a gender assigning function
gender <- function(names, femaleNames, maleNames)
{
        # create a placeholder dataframe
        data <- data.frame(names = names, gender = NA)
        # when name is found in vector of female names, assign gender female
        data$gender[sapply(names,"%in%", femaleNames)] <- "Female"
        # when name is found in vector of male names, assign gender male
        data$gender[sapply(names, "%in%", maleNames)] <- "Male"
        # return data frame with names and gender
        data
}

# see the output
gender(names, girls, boys)