rr-colnames

How to paste individual column names in front of column means in R?


I want to paste the names of the month in front of the monthly mean temperatures from my dataset but nothing I try works. I can't use very complex code because its for my class and we are starting from the basics. Anyone have a simple solution to this error?

Here is the full program i want to run:

for(i in raw[2:13]) {
  avg <- round(mean(i), digits = 1)
  monthavg <- cat(colnames(i), ":", avg, "ºC \n")
}

This is the result I want:

January: -9.2 ºC 
February: -7.3 ºC 
March: -3 ºC 
April: 4.2 ºC 
May: 9.6 ºC 
June: 13.4 ºC 
July: 16.4 ºC 
August : 15.4 ºC 
September: 10.7 ºC 
October: 5.4 ºC 
November: -2.3 ºC 
December: -6.8 ºC 

The "colnames" is the part that needs debugging. Below are the only 3 results I have managed to get from different variations I have tried:

First try:

> for(i in raw[2:13]) {
+   avg <- round(mean(i), digits = 1)
+   monthavg <- cat(month.name, ":", avg, "ºC \n")
+ }
January February March April May June July August September October November December : -9.2 ºC 
January February March April May June July August September October November December : -7.3 ºC 
January February March April May June July August September October November December : -3 ºC 
January February March April May June July August September October November December : 4.2 ºC 
January February March April May June July August September October November December : 9.6 ºC 
January February March April May June July August September October November December : 13.4 ºC 
January February March April May June July August September October November December : 16.4 ºC 
January February March April May June July August September October November December : 15.4 ºC 
January February March April May June July August September October November December : 10.7 ºC 
January February March April May June July August September October November December : 5.4 ºC 
January February March April May June July August September October November December : -2.3 ºC 
January February March April May June July August September October November December : -6.8 ºC 

2nd try:

> for(i in raw[2:13]) {
+   avg <- round(mean(i), digits = 1)
+   monthavg <- cat(colnames(i), ":", avg, "ºC \n")
+ }
 : -9.2 ºC 
 : -7.3 ºC 
 : -3 ºC 
 : 4.2 ºC 
 : 9.6 ºC 
 : 13.4 ºC 
 : 16.4 ºC 
 : 15.4 ºC 
 : 10.7 ºC 
 : 5.4 ºC 
 : -2.3 ºC 
 : -6.8 ºC 

3rd try:

> for(i in raw[2:13]) {
+   avg <- round(mean(i), digits = 1)
+   monthavg <- cat(colnames(raw[i]), ":", avg, "ºC \n")
+ }
 : -9.2 ºC 
Error in `[.default`(raw, i) : 
  only 0's may be mixed with negative subscripts

Solution

  • I'll just assume how your data.frame looks like based on your code since an example is not provided.

    
    raw = data.frame(
      Jan=rnorm(12),
      Feb=rnorm(12),
      Mar=rnorm(12)
    )
    

    You just need to change how you reference the columns during the loops. Usually I loop through a number instead of a dataframe for easier referencing.

    for(i in 1:3) {
      avg <- round(mean(raw[,i]), digits = 1)
      monthavg <- cat(colnames(raw[i]), ":", avg, "ºC \n")
    }
    

    Alternative usage of colnames:

    
    month.names=colnames(raw)
    
    for(i in 1:3) {
      
      month = month.names[i]
      
      avg <- round(mean(raw[,month]), digits = 1)
      monthavg <- cat(month, ":", avg, "ºC \n")
    }