Need help looping through list of data frame

I have a list of data frames and I want to loop through the list and within each element in that list (i.e., data frame) loop through the columns to transform that column. The input would look like

$`df1`
a b c
5 30 2
4 2 15
3 2 17

$df2
a b c
5 30 2
4 2 15
3 2 17

and the output would look like:

$`df1`
a b c
5.02 30.02 2
4.15 2.15 15
3.17 2.17 17

$df2
a b c
5.02 30.02 2
4.15 2.15 15
3.17 2.17 17

where the last column, c, is pasted or added (if you take 1/100 of it) to columns a and b in each data frame throughout the list. Note that there might be thousands of these data frames in the list - I just used a list of two data frames as an easy example.

Thanks in advance for any help!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <sstream>

int main() {
    std::ifstream fin("input_file");
    std::ofstream fout("output_file");
    std::string line;

    while (fin) {
        std::getline(fin, line);  // read frame name
        fout << line << '\n';
        std::getline(fin, line); // read a b c
        fout << line << '\n';

        for (int i = 0; i < 3; i++) {
            std::getline(fin, line);
            std::istringstream sin(line);
            double a, b, c;
            sin >> a >> b >> c;

            fout << a + c / 100 << ' '
                 << b + c / 100 << ' '
                 << c << '\n';
        }

        std::getline(fin, line); // read blank line
        if (fin)
            fout << line << '\n';
    }
}

Thank you!
No prob. Alternatively, this is exactly the kind of thing that awk was made for:
 
awk '/^[$a]|^\s*$/{print;next};{print $1+$3/100,$2+3/100,$3}' input_file > output_file

Last edited on
Topic archived. No new replies allowed.