0

Suppose I have a dataframe

     col1  
df=   1
      2
      3
      4

How do I get the following in R

      col1   col2
df=    1      
       2
       3
       4      10(total of col1)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
Saideep
  • 27
  • 3

1 Answers1

0

You can do :

df <- data.frame(col1 = 1:4)
df$col2 <- NA
df$col2[nrow(df)] <- sum(df$col1, na.rm = TRUE)
df
#  col1 col2
#1    1   NA
#2    2   NA
#3    3   NA
#4    4   10

Keeping other values in col2 as NA instead of blanks since blanks would turn the column to character.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213