I am trying something that should be simple, any hint on what is going on is very welcomed.
I have a large data frame with country imports from some municipalities.
It is because of how you're creating your dataframe. For example, c1
is character because a vector can only have one class. When you put them into a dataframe, those character vectors are further coerced to factor. Thus you're trying to run sum
on factors. You figured this out already, but then tried to convert factors to numeric, which is probably giving you nonsensical results.
The easy answer is to build your dataframe column-wise rather than row-wise, so you don't get into so many coercion problems.
Given the data you already have, this will solve your problem:
df[] <- lapply(df, function(x) type.convert(as.character(x)))
aggregate(. ~ V1, df, sum)
(Thanks to @AnandaMahto for the much cleaner way of doing that conversion than what I originally had.)
Result:
V1 V2 V3 V4 V5 V6 V7
1 Afghanistan 2 54 34.5 10.4 2 0
2 Albania 12 160 72.5 70.5 664 12