-2

I am new to R..hence this basic question. I cant get the last line of code to work. Can you take a look : )

# Importing an Excel CSV file
initial_import <- read.csv('~/my_r_studio_work/csv1.csv',header=TRUE,na.strings=c('NA','NaN','<NA>','na'))
ii_df<-data.frame(initial_import)

# Summing rows across horizontally
add_rows<-rowSums(ii_df[,3:5],na.rm=TRUE)

# Attaching this rowsum total to a column
ii_df_addrow<-cbind(ii_df,add_rows)

# Summing all columns 
sumcolumns<-colSums(ii_df_addrow[,3:6],na.rm=TRUE)

# Attaching these columns totals to ii_df_addrow data frame
attach_col_tots<-rbind(ii_df_addrow,sumcolumns)

The last line of this code throws and an error..I don't know how to fix it..Any suggestions?

lmo
  • 37,904
  • 9
  • 56
  • 69
RStudio2015
  • 11
  • 1
  • 3
  • 7
    What is the error? please post the result of `dput(initial_import)` here – Veerendra Gadekar Aug 28 '15 at 23:30
  • Welcome to SO. First of all you should read [here](http://stackoverflow.com/help/how-to-ask) about how to ask a good question; a good question has better changes to be solved and you to receive help. On the other hand a read of [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is also good. It explains how to create a reproducible example in R. Help users to help you by providing a piece of your data a desired output and things you have tried so far. – SabDeM Aug 29 '15 at 00:10

1 Answers1

0

The problem is here

sumcolumns<-colSums(ii_df_addrow[,3:6],na.rm=TRUE)

The sumcolumns has sum for only columns 3 to 6 so the number of columns in sumcolumns and ii_df_addrow are different. rbind requires same columns.

Rohit Das
  • 1,962
  • 3
  • 14
  • 23
  • A useful function is bind_rows() from dplyr. It lets you rbind when the columns don't all match. – lawyeR Aug 29 '15 at 12:16