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

Viewed 239

I am trying to update values in column 'PointID' in a dataset 'all.newpoint'

enter image description here

This is what 'all.newpoint' dataset looks like and I would want to update PointID into like A1, A2 ... according to the range that it falls into by referring site.lat, site.lon for range calculation.

enter image description here

when I run the below code block, it would give me an error.

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
                    }
                }
            }
        }
    }

it gives error like this and I don't know why it gives 'SiteName' exists but is invalid error.

[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")
0 Answers
Related