[R] Re : Adding column sum to new row in data frame

David Winsemius dwinsemius at comcast.net
Thu May 20 05:48:10 CEST 2010


On May 19, 2010, at 11:19 PM, Mohan L wrote:

>>
>>
>> How to safely avoid this warning massage?
>> Now I have <NA> instead of "Total" in last row State column. How to I
>> replace it as "Total"?
>>
>> Dear All,
>
> The below link provides a very good explanation of  "Creating factor
> variables" and way to avoid the warning message
> http://www.ats.ucla.edu/stat/R/modules/factor_variables.htm
>
> Now it works for me without warning message ( invalid factor level,  
> NAs
> generated ) according to the document above , I did  below to avoid  
> the
> warning:
>
>> data <- read.csv(file='ipsample.csv',sep=',' , header=TRUE)
>> data
>  State  Jan  Feb  Mar  Apr  May Jun
> 1   AAA    1    1    0    2    2   0
> 2   BBB 1298 1195 1212 1244 1158 845
> 3  CCC     0    0    0    1    2   1
> 4   DDD    5   11   17   15   10   9
> 5   EEE   18   28   27   23   23  16
> 6   FFF   68  152  184  135  111  86
>
>
>> a <- rbind(data, c("Total",apply(data[,-1], 2, sum, na.rm=TRUE)))
> Warning message:
> In `[<-.factor`(`*tmp*`, ri, value = "Total") :
>  invalid factor level, NAs generated
>
>> a
>  State  Jan  Feb  Mar  Apr  May Jun
> 1   AAA    1    1    0    2    2   0
> 2   BBB 1298 1195 1212 1244 1158 845
> 3  CCC     0    0    0    1    2   1
> 4   DDD    5   11   17   15   10   9
> 5   EEE   18   28   27   23   23  16
> 6   FFF   68  152  184  135  111  86
> 7  <NA> 1390 1387 1440 1420 1306 957
>
> We can see that instead of "Total", the label was <NA>. To do this
> correctly, I have added the new level, "Total", to the factor column
> data$State using the factor function with the levels argument. Then  
> I can
> finally add an element to the factor variable from the new level.  
> here is
> the steps
>
>> levels(data$State)
> [1] "AAA"  "BBB"  "CCC " "DDD"  "EEE"  "FFF"
>
>> data$State <- factor(data$State,levels=c(levels(data$State),"Total"))
>
>> data$State
> [1] AAA  BBB  CCC  DDD  EEE  FFF
> Levels: AAA BBB CCC  DDD EEE FFF Total
>
>> levels(data$State)
> [1] "AAA"   "BBB"   "CCC "  "DDD"   "EEE"   "FFF"   "Total"
>
>> x <- rbind(data, c("Total",apply(data[,-1], 2, sum, na.rm=TRUE)))
>
> Now the above works without warning.
>
>> x
>  State  Jan  Feb  Mar  Apr  May Jun
> 1   AAA    1    1    0    2    2   0
> 2   BBB 1298 1195 1212 1244 1158 845
> 3  CCC     0    0    0    1    2   1
> 4   DDD    5   11   17   15   10   9
> 5   EEE   18   28   27   23   23  16
> 6   FFF   68  152  184  135  111  86
> 7 Total 1390 1387 1440 1420 1306 957
>
> I think I am doing right. If  I miss understood anything. Please  
> guide me  I
> am beginer to R.

If you had instead used stringsAsFactors=FALSE with the read.table  
function, the "State" column would have been character rather than  
factor, and you would have avoided all those difficulties.

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list