In my dataframe I have a column which is the month/year of the latest available data (e.g. it currently says Dec-23 as that's the latest but next month it will say Jan-24). I want to calculate the mean of this column, but I don't want to have to go in and change the column name each time. The latest month is always column 26.
OrgCode |...| Oct-23 | Nov-23 | Dec-23
OrgA |...| 0.78 | 0.56 | 0.76
OrgB |...| 0.36 | 0.46 | 0.74
OrgC |...| 0.48 | 0.66 | 0.96
I have tried the following:
z_name = names(df[,26])
z = df$z_name
But get this error:
Warning message:
Unknown or uninitialised column: `z_name`
I also tried this approach:
mean.df.latest = df %>% mean(.[[26]])
But get this error:
Warning: argument is not numeric or logical: returning NA
You can do
df[,26] %>% mean(), or since it's always the LAST column you can dodf[,ncol(df)] %>% mean(). That last one works as follows:ncol(df)gives you the number of columns in your data frame (26) in your case, and then pass that number into the columns of thedf, remember that on a data frame you specify the row location in the first placedf[row, ]and column location in the second placedf[ ,column].