Add a constant value to all rows in a dataframe

2019-02-23 19:13发布

问题:

I want to add the number 1 to all the rows and columns of dataframe. Here's the dataframe:

df <- structure(list(units = c(422, 188, 314, 197, 2825, 3210, 450, 
117, 291, 161, 113, 259), quantity = c(361, 163, 298, 74, 1997, 
2803, 164, 62, 203, 140, 115, 269), KP = c(282, 194, 295, 102, 
1772, 1610, 151, 295, 126, 157, 191, 233), level = c(215, 62, 
215, 78, 1646, 635, 128, 65, 126, 402, 552, 281)), .Names = c("units", 
"quantity", "KP", "level"), row.names = c(NA, 12L), class = "data.frame")

which looks like:

   units quantity   KP level
1    422      361  282   215
2    188      163  194    62
3    314      298  295   215
4    197       74  102    78
5   2825     1997 1772  1646
6   3210     2803 1610   635
7    450      164  151   128
8    117       62  295    65
9    291      203  126   126
10   161      140  157   402
11   113      115  191   552
12   259      269  233   281

And my target is:

   units quantity   KP level
1    423      362  283   216
2    189      164  195    63
3    315      299  296   216
4    198       75  104    79
...
etc

Thank yall for your help..

回答1:

Simply df + 1 will do that for you.



标签: r dataframe add