Returning For Loop result as data frame

Hello again R family. I have a For loop algorithm, which works well to loop through my data and compute the maximum values in each column. However, I will like for the result to be returned as data frame. My code is shown below:

dfun <- function(){
for (i in 1:ncol(demo_df)){
  result <- max(demo_df[i])

}
df <- data.frame(variables = colnames(demo_df),value = result)
return(df)
}
dfun()

This code returns a data frame quite alright, but it returns just one max value, rather than the max values of all the columns. This is what I mean:

variables value
1       AGE    56
2   ADDRESS    56
3    INCOME    56
4    INCCAT    56
5       CAR    56
6    EMPLOY    56

The data set is attached looks like this:

AGE ADDRESS INCOME INCCAT
1    55      12     72      3
2    56      29    153      4
3    28       9     28      2
4    24       4     26      2
5    25       2     23      1
6    45       9     76      4
7    44      17    144      4
8    46      20     75      4
9    41      10     26      2
10   29       4     19      1
11   34       0     89      4
12   55      17     72      3
13   28       9     55      3
14   21       2     20      1
15   55       8    283      4
16   35       8     70      3
17   45       4     48      2
18   21       1     37      2
19   32       0     28      2
20   42       9    109      4
21   40      12    117      4
22   36       6     39      2
23   42      13     53      3
24   65      17     42      2
25   52       5     83      4
26   51      17    148      4
27   44       1     29      2
28   26       6     28      2
29   41      19     70      3
30   49      11     40      2
31   64      36    102      4
32   39       8     60      3
33   53       8     78      4
34   38      17     43      2
35   46       6     31      2
36   58       2     60      3
37   25       0     58      3
38   57      28     92      4
39   47      21     48      2
40   21       0     13      1
41   45      21     67      3
42   56       7    213      4
43   24       2     19      1
44   41      13     59      3
45   22       0     45      2
46   54       9     35      2
47   49       0     94      4
48   49      12     35      2
49   51       0     47      2
50   58      10    873      4
51   30      10     41      2
52   48      22    109      4
53   36      17     86      4
54   33       1     37      2
55   51       5     30      2
56   45       0     38      2
57   56      12     50      3
58   48       7     41      2
59   53      10     52      3
60   28       9     28      2
61   23       1     15      1
62   67       6     44      2
63   68      41     25      2
64   56       5     46      2
65   43       8    109      4
66   28       7     24      1
67   39       6     57      3
68   37      16     24      1
69   32       3     30      2
70   55      29     65      3
71   44      15     60      3
72   48      28    321      4
73   36       8     51      3
74   28       1     67      3
75   38      12     50      3
76   30       0     20      1
77   19       0     13      1
78   39       2     43      2
79   51      10     75      4
80   41      20    248      4
81   38       7     42      2
82   56      34    376      4
83   54       1     28      2
84   46       0     23      1
85   40      11     34      2
86   30       4     23      1
87   29       2     25      2
88   30      10     27      2
89   53       8    168      4
90   51      24    140      4
91   40      15     57      3
92   66      34     30      2
93   61      18     23      1
94   61       6     64      3
95   23       0     19      1
96   51      26     86      4
97   47       1     28      2
98   59      18    209      4
99   25       5     27      2
100  63      31    478      4
101  41       8     49      2
102  63      10    246      4
103  41       3     27      2
104  47      16     64      3
105  42       9     49      2
106  35       5     49      2
107  24       2     23      1
108  22       3     23      1
109  32      12     59      3
110  50      20     58      3
111  44       8     46      2
112  32      12     31      2
113  65      43     40      2
114  47      21    181      4
115  41      13     79      4
116  48      29    144      4
117  29       4     22      1
118  32       0     25      2
119  30       1     51      3
120  53      34    136      4
121  58      27    385      4
122  26       6     41      2
123  25       6     23      1
124  51      20     78      4
125  62       5     49      2
126  46       7     83      4
127  32      13     45      2
128  50       3     40      2
129  30       1     30      2
130  54      21     39      2
131  29       3     76      4
132  44      13     74      3
133  23       0     24      1
134  35       7     56      3
135  24       5     22      1
136  49      21    117      4
137  51       1    130      4
138  32       1     26      2
139  56       1     36      2
140  38      14     48      2
141  61       8     11      1
142  55      13    188      4
143  48       3     25      2
144  51       2     48      2
145  20       0     19      1
146  68      23     22      1
147  46      23     31      2
148  35      15     59      3
149  23       3     13      1
150  72      49     23      1
151  21       2     17      1
152  57      24    181      4
153  36       2     40      2
154  61      17    837      4
155  27       8     43      2
156  44      15    100      4
157  43       5    144      4
158  32      12     54      3
159  28       7     86      4
160  27       0     25      2
161  46      23     34      2
162  51      13     51      3
163  21       0     25      2
164  38       3     29      2
165  41       1     69      3
166  47      22    232      4
     CAR EMPLOY
1   37.0     23
2   76.0     35
3   13.9      4
4   13.0      0
5   11.3      5
6   37.3     13
7   72.1     23
8   37.1     29
9   13.0      8
10   9.6     10
11  44.4     12
12  36.1      2
13  28.2      1
14   9.6      0
15  77.4     11
16  35.4      9
17  25.0      7
18  18.4      0
19  14.2      2
20  54.0     20
21  59.8     19
22  19.2      0
23  25.9      6
24  21.3     24
25  40.9     24
26  73.7     10
27  14.3      5
28  14.0      1
29  35.1     17
30  20.1      8
31  49.0     22
32  30.2     11
33  38.3     17
34  20.9     11
35  16.2     15
36  29.1      1
37  28.8      4
38  46.6     25
39  24.0     23
40   6.4      0
41  34.1     20
42  70.6     30
43   9.6      0
44  28.9     17
45  21.3      0
46  18.1      7
47  47.6     17
48  17.4      4
49  23.3      3
50  73.6     34
51  20.0      4
52  55.4      8
53  41.9     12
54  18.3      3
55  14.6      6
56  18.8      4
57  24.6     11
58  19.7     15
59  26.2      8
60  14.3      6
61   7.3      2
62  21.7      2
63  12.2     10
64  23.5      9
65  53.7     20
66  11.9      2
67  27.8     12
68  11.6      7
69  14.7      7
70  32.7     10
71  30.6     14
72  78.2     11
73  25.0      3
74  32.9      7
75  25.6      9
76  10.3      3
77   6.6      0
78  21.3      8
79  37.8      6
80  71.5      6
81  20.8      3
82  75.5     35
83  14.0      1
84  11.7      1
85  16.7     14
86  11.7      3
87  12.1     10
88  13.4      3
89  87.1     24
90  69.6     32
91  28.1     12
92  15.2      4
93  11.5     27
94  32.6     25
95   9.5      0
96  43.6     10
97  13.9      8
98  75.3     22
99  13.1      2
100 72.4     45
101 23.6     14
102 65.2     28
103 13.5      4
104 32.5     23
105 24.2     12
106 24.5      9
107 11.6      1
108 11.9      0
109 29.2      5
110 29.9      8
111 22.9     18
112 15.0      1
113 20.4     10
114 90.3     21
115 39.0     16
116 70.9     14
117 11.1      0
118 12.3     13
119 25.9     14
120 66.3     21
121 77.8     34
122 20.3      0
123 11.0      0
124 38.3     14
125 24.6      7
126 40.0     14
127 22.3      7
128 20.7      1
129 14.7      9
130 19.4      7
131 37.7      4
132 37.1     12
133 11.9      0
134 28.4      1
135 11.1      0
136 57.8     16
137 64.7     18
138 13.3      7
139 17.9     13
140 21.9      1
141  5.4     14
142 94.3     33
143 12.7      5
144 24.0      7
145  9.6      1
146 10.8     24
147 15.3     11
148 31.0      3
149  6.4      1
150 11.5     11
151  8.5      0
152 90.0     14
153 19.3      8
154 75.7     42
155 20.6      0
156 49.7     27
157 72.5     24
158 27.1      6
159 42.4      0
160 12.3      1
161 16.6     14
162 26.3     14
163 12.6      0
164 14.5      5
165 35.1     18
166 72.6     22

Please as always, your help and support will be highly appreciated. Thanks guys.

library(tidyverse)

(done1 <- iris %>% summarise(
               across(where(is.numeric),
                      max)))

   (done2 <- pivot_longer(done1,
                          cols = everything()))
1 Like

Nir already showed you a solution. To add to that, your code fails because of this:

You're assigning maximum of all column to a single scalar variable, and that ends up being replaced immediately in next iteration.

The correct way to do it should be to initialise an empty vector, preferably with the pre-allocated dimension, and updated each of its elements one by one. I'll leave it up to you to try it.

But for this particular case, there will be better solutions. One is above already, and there can be solutions using lapply, sapply, purrr::map_dbl, etc a lot to get column maximums. Prefer those over for loop, but IMHO be familiar it's use.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.