内部错误:索引“SiteName”存在,但在 R 中无效

Internal error: index 'SiteName' exists but is invalid in R

提问人:Heeone Lee 提问时间:10/20/2021 最后编辑:Heeone Lee 更新时间:10/21/2021 访问量:515

问:

我正在尝试更新数据集“all.newpoint”中“PointID”列中的值

enter image description here

这就是“all.newpoint”数据集的样子,我想将 PointID 更新为 A1、A2 ...根据它所属的范围,通过参考 site.lat、site.lon 进行范围计算。

enter image description here

当我运行下面的代码块时,它会给我一个错误。

all.newpoint <- all.newdata
for (i in 1:nrow(site.lat))
    {
    sitename <- site.lat[[i,1]]
    min.lat <- site.lat[[i,3]]
    min.lon <- site.lon[[i,3]]
    latTh <- site.lat[[i,5]]
    lonTh <- site.lon[[i,5]]
    df <- all.new[SiteName == sitename]
    print('---')
    Latnames <- c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K','L')
    for (j in 1:30)
        {
        df1 <- df[Lat >= as.numeric(min.lat+latTh*(j-1)) & Lat < as.numeric(min.lat+latTh*j)]
        if (nrow(df1) > 0)
            {
            Lat.name <- Latnames[1]
            Latnames <- Latnames[-1]
            Lonnames <- c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11','12')
            if (length(Latnames) ==0 ) break
            for (l in 1:30)
                {
                df2 <- df1[Lon >= as.numeric(min.lon+lonTh*(l-1)) & Lon < as.numeric(min.lon+lonTh*l)]
                if (nrow(df2) > 0)
                    {
                    print(as.numeric(min.lat+latTh*(j-1)))
                    print(min.lat+latTh*j)
                    print(as.numeric(min.lon+lonTh*(l-1)))
                    print(as.numeric(min.lon+lonTh*l))
                    Lon.name <- Lonnames[1]
                    Lonnames <- Lonnames[-1]
                    point.id <- paste0(Lat.name, Lon.name)
                    print(point.id)
                    all.new <- anti_join(all.new, df2)
                    all.newpoint[which(all.newdata$Lat %in% unique(df2$Lat) & all.newdata$Lon %in% unique(df2$Lon))]$PointID <- point.id
                    df <- all.new[SiteName == sitename]
                    df1 <- df[Lat >= as.numeric(min.lat+latTh*(j-1)) & Lat < as.numeric(min.lat+latTh*j)]
                    if (nrow(all.new[SiteName == sitename]) == 0) break
                    }
                }
            }
        }
    }

它给出这样的错误,我不知道为什么它给出“SiteName”存在,但这是无效的错误。

[1] "---"
[1] 34.63306
[1] 34.66967
[1] 128.5905
[1] 128.6187
[1] "A1"
Joining, by = c("Year", "Month", "SiteName", "PointID", "Lat", "Lon", "Depth", "Temp_S", "Temp_B", "Sal_S", "Sal_B", "pH_S", "pH_B", "DO_S", "DO_B", "COD_S", "COD_B", "NH4_S", "NH4_B", "NO3_S", "NO3_B", "NO2_S", "NO2_B", "DIN_S", "DIN_B", "TN_S", "TN_B", "DIP_S", "DIP_B", "TP_S", "TP_B", "Si_S", "Si_B", "Chla_S", "Chla_B", "SS_S", "SS_B", "Transparency")
Error in getindex(x, names(x)[xcols]): Internal error: index 'SiteName' exists but is invalid
Traceback:

1. all.new[SiteName == sitename]
2. `[.data.table`(all.new, SiteName == sitename)
3. bmerge(i, x, leftcols, rightcols, roll, rollends, nomatch, mult, 
 .     ops, verbose = verbose)
4. getindex(x, names(x)[xcols])
5. stop("Internal error: index '", name, "' exists but is invalid")
R DataFrame DataTable 数据集 数据操作

评论

0赞 Ronak Shah 10/20/2021
如果您创建一个可重复的小示例以及预期的输出,则会更容易提供帮助。阅读有关如何给出可重复示例的信息。图像不是共享数据/代码的正确方式。

答: 暂无答案