Sweep Over An Dataframe

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# create a dataframe with simulated values
x <- runif(100)
y <- runif(100)
z <- runif(100)
a <- runif(100)
data <- data.frame(x, y, z, a)
rm(x, y, z, a)
# Add 10 to the first column, 20 to the second column, 30 to the third column, 40 to the fourth column. Columns are denoted with the "2"
sweep(data, 2, c(10, 20, 30, 40), "+")
           x        y        z        a
1   10.32730 20.32033 30.25435 40.35822
2   10.25235 20.01114 30.92937 40.41954
3   10.35791 20.53621 30.51055 40.05875
4   10.11858 20.23212 30.17816 40.27137
5   10.88790 20.99958 30.60182 40.20856
6   10.52219 20.43288 30.90038 40.11521
7   10.46691 20.69662 30.42834 40.56927
8   10.26317 20.73948 30.38198 40.44033
9   10.22629 20.50134 30.52825 40.14349
10  10.35842 20.30308 30.59516 40.86080
11  10.96218 20.40019 30.67054 40.23832
12  10.69216 20.56607 30.13535 40.20793
13  10.88016 20.44120 30.96720 40.95606
14  10.88235 20.17180 30.73084 40.43032
15  10.03777 20.98083 30.44728 40.52844
16  10.88287 20.11302 30.20767 40.27113
17  10.64171 20.70630 30.77119 40.32028
18  10.03359 20.09994 30.69826 40.25316
19  10.47795 20.13006 30.31004 40.16694
20  10.32405 20.63986 30.27047 40.78593
21  10.97502 20.14313 30.03588 40.05490
22  10.90446 20.05095 30.08117 40.76391
23  10.71511 20.06212 30.10469 40.06789
24  10.92001 20.61362 30.90136 40.76595
25  10.60598 20.24580 30.33061 40.92882
26  10.22627 20.07714 30.82103 40.21735
27  10.35566 20.06530 30.93909 40.91805
28  10.40162 20.17826 30.79633 40.42704
29  10.17046 20.57119 30.40088 40.23268
30  10.63219 20.70219 30.36659 40.32080
31  10.89678 20.47896 30.73640 40.11156
32  10.92914 20.15821 30.98486 40.57949
33  10.87023 20.70825 30.05313 40.73828
34  10.33950 20.39289 30.76355 40.92854
35  10.67925 20.77507 30.00954 40.67655
36  10.80570 20.03596 30.47324 40.91835
37  10.58231 20.69349 30.19653 40.60302
38  10.95704 20.28324 30.62937 40.02016
39  10.45236 20.76670 30.73899 40.25480
40  10.10766 20.58719 30.86107 40.13780
41  10.00444 20.68638 30.62684 40.45284
42  10.42376 20.43846 30.14995 40.13457
43  10.27377 20.55315 30.12707 40.28277
44  10.92104 20.36958 30.41070 40.17274
45  10.02524 20.78526 30.28361 40.76992
46  10.04650 20.72938 30.36932 40.88512
47  10.01691 20.65482 30.31852 40.33411
48  10.06295 20.15335 30.82940 40.57323
49  10.07610 20.60222 30.06898 40.34765
50  10.96166 20.44878 30.99801 40.92482
51  10.57523 20.20867 30.46799 40.36589
52  10.27001 20.36986 30.55987 40.46065
53  10.67929 20.36916 30.82538 40.89647
54  10.19466 20.69508 30.81382 40.97260
55  10.11495 20.53541 30.22398 40.07807
56  10.02986 20.48134 30.44946 40.51284
57  10.27442 20.33044 30.05853 40.47423
58  10.61156 20.30363 30.74917 40.66185
59  10.93385 20.73686 30.07704 40.67739
60  10.75203 20.77763 30.51821 40.94442
61  10.68343 20.27160 30.65755 40.09830
62  10.24891 20.00158 30.07434 40.76243
63  10.21683 20.22156 30.21365 40.52795
64  10.67541 20.40373 30.50563 40.57624
65  10.58832 20.82957 30.85743 40.50789
66  10.48336 20.41940 30.64045 40.20400
67  10.76902 20.67303 30.65093 40.35683
68  10.09627 20.73701 30.12208 40.94728
69  10.17750 20.46018 30.00964 40.71895
70  10.30874 20.49484 30.15179 40.87569
71  10.48646 20.73930 30.36417 40.01011
72  10.69185 20.28898 30.55007 40.92272
73  10.82433 20.13282 30.86382 40.58866
74  10.00377 20.70401 30.23805 40.19058
75  10.45241 20.81022 30.75468 40.08258
76  10.33948 20.02256 30.42336 40.25027
77  10.80330 20.70447 30.27600 40.52746
78  10.48710 20.73166 30.12767 40.30538
79  10.01032 20.49381 30.59134 40.34080
80  10.09500 20.88171 30.94042 40.79542
81  10.35373 20.75203 30.56967 40.75710
82  10.44146 20.40917 30.41113 40.89054
83  10.10264 20.94804 30.53518 40.59626
84  10.79171 20.83813 30.33770 40.19323
85  10.12137 20.77931 30.70078 40.07897
86  10.59251 20.52565 30.92419 40.31978
87  10.42809 20.43321 30.41216 40.98018
88  10.36346 20.97021 30.17975 40.33618
89  10.45994 20.89540 30.82994 40.31142
90  10.18585 20.59533 30.52392 40.63472
91  10.40786 20.57543 30.49794 40.08891
92  10.13158 20.29264 30.13490 40.06497
93  10.00396 20.77002 30.16856 40.35595
94  10.04839 20.46230 30.70012 40.34182
95  10.74123 20.42913 30.37626 40.24870
96  10.52422 20.26828 30.23467 40.23621
97  10.92283 20.11496 30.79288 40.93147
98  10.83929 20.60866 30.25455 40.10090
99  10.45022 20.43363 30.35828 40.81281
100 10.48697 20.86386 30.76933 40.27158