向数据框中的所有行添加常量值 [英] Add a constant value to all rows in a dataframe

查看:60
本文介绍了向数据框中的所有行添加常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数字 1 添加到数据框的所有行和列。以下是数据框:

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")

如下所示:

   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

我的目标是:

   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

感谢您的帮助。.

推荐答案

简单地 df + 1 会为您做到这一点。

Simply df + 1 will do that for you.

这篇关于向数据框中的所有行添加常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆