排查函数“数字表达式有 6 个元素:仅使用第一个元素”中的错误

问题描述 投票:0回答:1

我有以下示例数据

example_data <- data.frame(
  ID = 1:6,
  Month.Of.birth = c("September", "April", "December", "June", "April", "September"),
  year.of.birth = c(1942, 1942, 1938, 1946, 1944, 1946)
) 

我试图为每个日期随机分配一年中的某一天,以给出出生日期。我构建了以下代码,该代码最初有效,然后我将其放入完整代码中,现在在 1:num_days_in_month 中生成错误警告: 数值表达式有 6 个元素:仅使用第一个元素

有谁知道为什么吗?

generate_random_day_of_birth <- function(year, month) {
  if (is.na(year) || is.na(month)) {
    return(NA)
  }
  

  leap_year <- year %% 4 == 0 & (year %% 100 != 0 | year %% 400 == 0)
  
  num_days_in_month <- ifelse(month == "January", 31,
                              ifelse(month == "February" & !leap_year, 28,
                                     ifelse(month == "February" & leap_year, 29,
                                            ifelse(month %in% c("April", "June", "September", "November"), 30, 31))))
  
  random_day <- sample(1:num_days_in_month, 1)
  
  return(random_day)
}

example_data$day.of.birth <- generate_random_day_of_birth(example_data$year.of.birth, example_data$Month.Of.birth)
r dataframe data-cleaning
1个回答
0
投票

试试这个:

library(tidyverse)
# ------------------

set.seed(100)
example_data %>% 
  rowwise() %>% 
  mutate(
    Month.Of.birth = fct(Month.Of.birth, levels = month.name),
    date = ym(str_glue("{year.of.birth}, {Month.Of.birth}")),
    date = ymd(str_glue("{year.of.birth}, {Month.Of.birth} {sample(1:days_in_month(date), 1)}")))

输出:

# A tibble: 6 × 4
# Rowwise: 
     ID Month.Of.birth year.of.birth date      
  <int> <fct>                  <dbl> <date>    
1     1 September               1942 1942-09-10
2     2 April                   1942 1942-04-23
3     3 December                1938 1938-12-06
4     4 June                    1946 1946-06-16
5     5 April                   1944 1944-04-19
6     6 September               1946 1946-09-25
© www.soinside.com 2019 - 2024. All rights reserved.