Add a constant value to all rows in a dataframe

微笑、不失礼 提交于 2019-12-19 22:03:43

问题


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.



来源:https://stackoverflow.com/questions/28766443/add-a-constant-value-to-all-rows-in-a-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!