'Error when renaming columns with plyr in a loop in R
I found similar error but it seems the problem is not the same.
for (i in colnames(t[,4:ncol(t)])) {
for (j in poids_teff$station) {
if (i == j) {
t <- plyr::rename(t, c(i = poids_teff$nom_simplifie[poids_teff$station==j]))
}
}
}
Here is my error :
The following from
values were not present in x
: i
...
The following from
values were not present in x
: i
It seems the value passes as a string in the plyr when it should be a column name. I tried printing i and j to check and i had the right names.
I also tried dplyr but it says that the length does not match (but it does).
I checked the types and i and j are both characters. Printing i then poids_teff$nom_simplifie[poids_teff$station==j] gives this :
[1] "AGEN-LA GARENNE"
[1] "AGEN"
[1] "AUXERRE"
[1] "AUXERRE"
[1] "BALE-MULHOUSE"
[1] "MULHOUSE"
Head of poids_teff gives this :
nom_simplifie station J Jmoins1 Jmoins2
AGEN AGEN-LA GARENNE 0.64 0.24 0.12
AUXERRE AUXERRE 0.64 0.24 0.12
MULHOUSE BALE-MULHOUSE 0.64 0.24 0.12
Head of i gives this :
year month day AGEN-LA GARENNE AUXERRE BALE-MULHOUSE
1: 1949 1 1 6.9 NA 2.8
2: 1949 1 2 5.9 NA 1.3
3: 1949 1 3 2.9 NA 2.6
When i check typeof t and poids_teff they are bost lists.
Here is something that worked in python and that i'm trying to translate :
# for i in df_70.columns:
# for j in poids_teff['nom_70 ans']:
# if (i == j):
# df_70 = df_70.rename(columns={i:poids_teff['nom_simplifie'][poids_teff.set_index('nom_70 ans').index.get_loc(j)]})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|