I'm facing a peculiar issue in R. I need to avoid the autogeneration of column name after using the count() and aggregate() functions. If the code is run, on a machine which has some other base language, R assigns a column name which is different and thus my code throws an error.
What I wrote:
aggregated_data <- aggregate(data$diff, by = list(P_key = data$P_key), FUN=sum)
names(aggregated_data)[names(aggregated_data) == "x"] <- "d_sum"
count_data <- count(data, P_key)
names(count_data)[names(count_data) == "n"] <- "Obs"
The variable "x" and "n" are auto generated and change if the language of the computer is different. I need a way through which I can directly assign "d_sum" and "Obs" respectively, and avoid this issue.
The data (before renaming - for the aggregate function):
P_key x
1 115.770.11.5 21065
2 115.882.KJ.1 223451
3 115.883.KJ.1 47847
4 616.222.11.1 337464
The data (after explicitly renaming the 'x' column - for the aggregate function):
P_key d_sum
1 115.770.11.5 21065
2 115.882.KJ.1 223451
3 115.883.KJ.1 47847
4 616.222.11.1 337464
I need to avoid the auto generated column name "x", and assign it as "d_sum". Hence, avoiding the explicit renaming in the second line of the code.
Any help would be immensely appreciated.