Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复未知或未初始化的列:R tidyverse中的错误_R_Tidyverse_Tibble - Fatal编程技术网

如何修复未知或未初始化的列:R tidyverse中的错误

如何修复未知或未初始化的列:R tidyverse中的错误,r,tidyverse,tibble,R,Tidyverse,Tibble,今天我遇到了一个非常持久、麻烦的“未知或未初始化列:'gdd_runsum'”错误。我已经查找了可能导致错误的代码错误或问题,但找不到任何错误或问题。实际上,我在几天前运行了相同的代码,没有出现任何问题。我的代码中是否存在导致此错误的内容,告诉我在代码中找不到的列名未初始化 corn <- read.csv('I:\\HATCH_Project\\Corn.csv') corn <- corn[corn$Single == 'Yes',] corn$Count

今天我遇到了一个非常持久、麻烦的“未知或未初始化列:'gdd_runsum'”错误。我已经查找了可能导致错误的代码错误或问题,但找不到任何错误或问题。实际上,我在几天前运行了相同的代码,没有出现任何问题。我的代码中是否存在导致此错误的内容,告诉我在代码中找不到的列名未初始化

    corn <- read.csv('I:\\HATCH_Project\\Corn.csv')
    corn <- corn[corn$Single == 'Yes',]
    corn$County[which(corn$County == 'Mcpherson')] <- 'McPherson'
    corn$County[which(corn$County == 'Mccook')] <- 'McCook'
    corn_df_values <- data.frame(corn$Year,corn$County,corn$Value); colnames(corn_df_values) <- c('Year','County','Yield')
    
    corn_final <- corn_df_values %>% pivot_wider(names_from = County, values_from = Yield)
    corn_final <- corn_final[order(corn_final$Year),]
    corn_final_wide <- corn_final %>% tibble %>% select("Year", sort(colnames(.)))
    corn_final_long <- corn_final_wide %>% pivot_longer(!Year, names_to = 'County', values_to = 'Yield')
    corn_final_long <- corn_final_long[-which(is.na(corn_final_long$Yield)),]
    corn_by_county <- corn_final_long %>% group_by(County) %>% mutate(n = n())
    corn_by_county <- corn_by_county %>% group_by(County,n) %>% nest()

    gdd_runsum_df <- tibble(Date = seq(as.Date('2020-04-01'),as.Date('2020-11-01'),by='day'))
    gdd_runsum_df$month <- month(gdd_runsum_df$Date); gdd_runsum_df$day <- day(gdd_runsum_df$Date)
    gdd_runsum_df$runsum1 <- NA; gdd_runsum_df$runsum5 <- NA; gdd_runsum_df$runsum10 <- NA; gdd_runsum_df$runsum15 <- NA; gdd_runsum_df$runsum20 <- NA; 
    gdd_runsum_df$runsum25 <- NA; gdd_runsum_df$runsum30 <- NA; gdd_runsum_df$runsum35 <- NA; gdd_runsum_df$runsum40 <- NA; gdd_runsum_df$runsum45 <- NA;
    gdd_runsum_df$runsum50 <- NA; gdd_runsum_df$runsum60 <- NA; gdd_runsum_df$runsum70 <- NA; gdd_runsum_df$runsum75 <- NA; gdd_runsum_df$runsum80 <- NA; gdd_runsum_df$runsum90 <- NA;

    for (cty in 1:66){
      yield_model <- lm(corn_by_county$data[[cty]]$Yield ~ corn_by_county$data[[cty]]$Year)
      corn_by_county$data[[cty]]$Det_Yield <- as.numeric(resid(yield_model))}
    
    #GDD
    corn_by_county <- corn_by_county %>% mutate(gdd_min_runsum = list(as_tibble(gdd_runsum_df))) 
    corn_by_county <- corn_by_county %>% mutate(gdd_mean_runsum = list(as_tibble(gdd_runsum_df)))
    corn_by_county <- corn_by_county %>% mutate(gdd_median_runsum = list(as_tibble(gdd_runsum_df))) 
    corn_by_county <- corn_by_county %>% mutate(gdd_max_runsum = list(as_tibble(gdd_runsum_df))) 
    #GDDmod
    corn_by_county <- corn_by_county %>% mutate(gddmod_min_runsum = list(as_tibble(gdd_runsum_df))) 
    corn_by_county <- corn_by_county %>% mutate(gddmod_mean_runsum = list(as_tibble(gdd_runsum_df)))
    corn_by_county <- corn_by_county %>% mutate(gddmod_median_runsum = list(as_tibble(gdd_runsum_df))) 
    corn_by_county <- corn_by_county %>% mutate(gddmod_max_runsum = list(as_tibble(gdd_runsum_df))) 

corn您能否只共享导致错误的相关代码行,而不是共享整个脚本?我共享了脚本,因此您可以自己运行脚本并获取错误。不止一行给出了错误。当我运行脚本时,我在
for
loop
error in corn\u by_county$data[[cty]]:下标越界
。你有同样的错误吗?我没有得到
“未知或未初始化的列:'gdd_runsum'”
而不是1:66,它应该是1:50,因为这是一个小数据示例。当我打开一个新的R脚本并将代码粘贴到新脚本中时,它工作正常。也许我只需要写一个新的.R脚本。啊。令人沮丧的。谢谢你的努力!
junk <- cat(capture.output(dput(colnames(corn_by_county)[4:11])))


corn <- structure(list(Year = c(2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L), State = c("SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", "SOUTH DAKOTA", 
"SOUTH DAKOTA", "SOUTH DAKOTA"), Ag_District = c("CENTRAL", "CENTRAL", 
"CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", "EAST CENTRAL", "EAST CENTRAL", 
"EAST CENTRAL", "EAST CENTRAL", "NORTH CENTRAL", "NORTH CENTRAL", 
"NORTH CENTRAL", "NORTH CENTRAL", "NORTH CENTRAL", "NORTHEAST", 
"NORTHEAST", "NORTHEAST", "NORTHWEST", "NORTHWEST", "SOUTH CENTRAL", 
"SOUTH CENTRAL", "SOUTH CENTRAL", "SOUTHEAST", "SOUTHEAST", "SOUTHEAST", 
"SOUTHEAST", "SOUTHEAST", "SOUTHEAST", "SOUTHEAST", "SOUTHEAST", 
"CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", 
"CENTRAL", "CENTRAL", "EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", 
"EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", 
"EAST CENTRAL", "EAST CENTRAL", "NORTH CENTRAL", "NORTH CENTRAL", 
"NORTH CENTRAL", "NORTH CENTRAL", "NORTH CENTRAL", "NORTH CENTRAL", 
"NORTH CENTRAL", "NORTH CENTRAL", "NORTHEAST", "NORTHEAST", "NORTHEAST", 
"NORTHEAST", "NORTHEAST", "NORTHEAST", "NORTHEAST", "NORTHWEST", 
"NORTHWEST", "NORTHWEST", "SOUTH CENTRAL", "SOUTH CENTRAL", "SOUTH CENTRAL", 
"SOUTH CENTRAL", "SOUTHEAST", "SOUTHEAST", "SOUTHEAST", "SOUTHEAST", 
"SOUTHEAST", "SOUTHEAST", "SOUTHEAST", "SOUTHEAST", "SOUTHWEST", 
"SOUTHWEST", "SOUTHWEST", "WEST CENTRAL", "WEST CENTRAL", "CENTRAL", 
"CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", "CENTRAL", 
"EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", 
"EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", "EAST CENTRAL", 
"EAST CENTRAL", "EAST CENTRAL"), Ag_District_Code = c(50L, 50L, 
50L, 50L, 50L, 50L, 60L, 60L, 60L, 60L, 20L, 20L, 20L, 20L, 20L, 
30L, 30L, 30L, 10L, 10L, 80L, 80L, 80L, 90L, 90L, 90L, 90L, 90L, 
90L, 90L, 90L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 60L, 60L, 
60L, 60L, 60L, 60L, 60L, 60L, 60L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 10L, 10L, 10L, 80L, 
80L, 80L, 80L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 70L, 70L, 
70L, 40L, 40L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 60L, 60L, 60L, 
60L, 60L, 60L, 60L, 60L, 60L, 60L), County = c("Aurora", "Brule", 
"Buffalo", "Hand", "Jerauld", "Other (Combined) Counties", "Lake", 
"Mccook", "Minnehaha", "Other (Combined) Counties", "Brown", 
"Edmunds", "Mcpherson", "Other (Combined) Counties", "Spink", 
"Deuel", "Grant", "Other (Combined) Counties", "Other (Combined) Counties", 
"Ziebach", "Lyman", "Other (Combined) Counties", "Tripp", "Bon Homme", 
"Charles Mix", "Douglas", "Lincoln", "Other (Combined) Counties", 
"Turner", "Union", "Yankton", "Aurora", "Beadle", "Brule", "Hand", 
"Hughes", "Hyde", "Jerauld", "Other (Combined) Counties", "Brookings", 
"Kingsbury", "Lake", "Mccook", "Miner", "Minnehaha", "Moody", 
"Other (Combined) Counties", "Sanborn", "Brown", "Campbell", 
"Edmunds", "Faulk", "Mcpherson", "Potter", "Spink", "Walworth", 
"Clark", "Deuel", "Grant", "Hamlin", "Marshall", "Other (Combined) Counties", 
"Roberts", "Corson", "Other (Combined) Counties", "Ziebach", 
"Gregory", "Lyman", "Other (Combined) Counties", "Tripp", "Charles Mix", 
"Clay", "Hutchinson", "Lincoln", "Other (Combined) Counties", 
"Turner", "Union", "Yankton", "Bennett", "Custer", "Other (Combined) Counties", 
"Meade", "Other (Combined) Counties", "Aurora", "Beadle", "Brule", 
"Hand", "Hughes", "Other (Combined) Counties", "Sully", "Brookings", 
"Davison", "Hanson", "Kingsbury", "Lake", "Mccook", "Miner", 
"Minnehaha", "Moody", "Sanborn"), Commodity = c("CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", "CORN", 
"CORN", "CORN"), Value = c(133.9, 147.6, 133.5, 142.2, 142.6, 
128.8, 145.5, 129.3, 161.1, 138.7, 164.2, 133.4, 147.1, 138.9, 
152.9, 144, 146.3, 157.8, 116.4, 97.5, 131, 117.6, 119.1, 160.6, 
148.7, 149, 163.8, 161.9, 161.1, 178.7, 170, 164.3, 159.7, 168.9, 
138.3, 106, 125.7, 147.5, 101.3, 198.9, 184.5, 192.7, 177.9, 
178.7, 191.5, 194.1, 171.6, 167.2, 155.4, 129.6, 117.3, 103.2, 
108.7, 109, 160, 146.5, 177.5, 190.6, 185.1, 191.8, 184.6, 178, 
191.8, 91.9, 78.1, 78.8, 157.4, 120.7, 96.3, 124.7, 177.7, 179.6, 
166.8, 161.9, 165.1, 176.3, 186.2, 175.6, 128.7, 100, 98.2, 87.8, 
59, 122.1, 137.7, 106.5, 117, 100.7, 101.5, 100.3, 188.2, 127.2, 
152.9, 167.7, 183.8, 171.1, 163.4, 185.9, 187.7, 154.3), Single = c("Yes", 
"Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", 
"Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", "No", "Yes", 
"Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "No", 
"Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", 
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes", 
"Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")), row.names = c(NA, 
100L), class = "data.frame")