Home > Software engineering >  Can someone help me to solve this, need to create function to double the column sum
Can someone help me to solve this, need to create function to double the column sum

Time:06-02

I am missing 1 line to show the 3 column, can someone help.

 df_dummy = data.frame(ID = c(1001:1010), 
                      INT1 = sample(x = c(1:200), size = 10, replace = T)
)

col_double = function(df, col){
   df[,'double'] = df[,col]*2
}

col_double(df = df_dummy, col = 'INT1')

CodePudding user response:

In the function define the return object df:

col_double = function(df, col){
  df[,'double'] <- df[,col]*2
  df
}
    ID INT1 double
1  1001  168    336
2  1002  154    308
3  1003  198    396
4  1004   89    178
5  1005  197    394
6  1006    4      8
7  1007   53    106
8  1008  108    216
9  1009  138    276
10 1010   75    150
  • Related