以下是示例数据
stfips <- c("39","39","39")
year <- c("2023", "2023","2023")
industry_code <- c(112, 113, 114)
first_quarter_establishments <- c(987,654,321)
county <- data.frame(stfips, year, industry_code, first_quarter_establishments)
手头的任务是创建一个名为Period的新列,其值为01.01的原因是因为它代表了第一季度.如果第四列的名称中有单词"Second",则句点将为"02",依此类推.以下是我从ChatGPT上得到的信息.错误如下所示.你知道我将如何根据专栏的措辞创建这个时期的专栏吗?
first_columns <- grepl("first", names(county), ignore.case = TRUE)
county$period <- ifelse(first_columns, "01", "")
Error in `$<-.data.frame`(`*tmp*`, period, value = c("", "", "", "01")) :
replacement has 4 rows, data has 3
期望的最终结果
stfips year industry_code first_quarter_establishments period
39 2023 112 987 01
39 2023 113 654 01
39 2023 114 321 01