Can't figure out why I get this error. I tried everything. Please help

Hi everyone,
I am a new R user. I am running into a problem, which I can't resolve. Can someone please look at my code below and tell me why I get the following error:
"Error: Assigned data sum(temp$consultatop)/nrow(temp) must be compatible with existing data.
:information_source: Error occurred for column consultatop.
x Can't convert from to due to loss of precision.

  • Locations: 1.""

My Code:

First, list of unique alliance

alliances <- unique(bc_ext$atopid)
allience <- as.data.frame(alliances)

for(i in 1:length(alliances)){
## subset data on all states in alliance
temp <- filter(bc_ext, atop$atopid == alliances[i])
all_alliances[i, 1] <- temp$atopid[1]
all_alliances[i, 2] <- sum(temp$macowe)/nrow(temp)
all_alliances[i, 3] <- sum(temp$maconc)/nrow(temp)
all_alliances[i, 4] <- sum(temp$maconp)/nrow(temp)
all_alliances[i, 5] <- sum(temp$nmacowe)/nrow(temp)
all_alliances[i, 6] <- sum(temp$nmaroir)/nrow(temp)
all_alliances[i, 7] <- sum(temp$condother)/nrow(temp)
all_alliances[i, 8] <- sum(temp$renounceok)/nrow(temp)
all_alliances[i, 9] <- sum(temp$renounceproh)/nrow(temp)
all_alliances[i, 10] <- sum(temp$renouncecond)/nrow(temp)
all_alliances[i, 11] <- sum(temp$offenseatop)/nrow(temp)
all_alliances[i, 12] <- sum(temp$defenseatop)/nrow(temp)
all_alliances[i, 13] <- sum(temp$neutatop)/nrow(temp)
all_alliances[i, 14] <- sum(temp$consultatop)/nrow(temp)
all_alliances[i, 15] <- sum(temp$milcontact)/nrow(temp)
all_alliances[i, 16] <- sum(temp$commondef)/nrow(temp)
all_alliances[i, 17] <- sum(temp$intcom)/nrow(temp)
all_alliances[i, 18] <- sum(temp$milaid)/nrow(temp)
all_alliances[i, 19] <- sum(temp$base)/nrow(temp)
all_alliances[i, 20] <- sum(temp$contrib)/nrow(temp)
all_alliances[i, 21] <- sum(temp$organ1)/nrow(temp)
all_alliances[i, 22] <- sum(temp$ecaid)/nrow(temp)
all_alliances[i, 23] <- sum(temp$secrecy)/nrow(temp)
}

As a beginner to R you may benefit from studying this useful book.
https://r4ds.had.co.nz/
Particularly chapter 5

1 Like

Thank you for sharing this. I'm replicating someone else's R code and I couldn't figure out why they got the result but i didn't.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.