How to save multiple values of a variable in a json file in R

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

How to save multiple values of a variable in a json file in R

ioanna
hello everyone,

and happy new year!

I have this problem: I want to save the name of the 'countries', the 'taxonomy_gem' and the 'minimum_im' and 'maximum_im' . The problem is that there are several names of countries.  How can i transfer the information from the json file to an R data.frame? See below for the json file.

Best,
ioanna



{
    "pk": 670,
    "model": "vulnerability.generalinformation",
    "fields": {
        "category": "Structure class",
        "article_title": "A GLOBAL DATABASE OF VULNERABILITY MODELS FOR SEISMIC RISK ASSESSMENT",
        "name": "CR/LFINF/DUL/H:2",
        "publication_conference_name": "16EECE",
        "llrs": null,
        "material": null,
        "web_link": "",
        "owner": {
            "pk": 1900,
            "model": "people.profile",
            "fields": {
                "username": "lmartins",
                "first_name": "",
                "last_name": "",
                "email": "[hidden email]"
            }
        },
        "general_comments": "",
        "geo_applicability": {
            "pk": 669,
            "model": "vulnerability.geoapplicability",
            "fields": {
                "general_information": 670,
                "area": "",
                "countries": [
                    {
                        "pk": "CIV",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Cte d'Ivoire"
                        }
                    },
                    {
                        "pk": "CMR",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Cameroon"
                        }
                    },
                    {
                        "pk": "SDN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Sudan"
                        }
                    },
                    {
                        "pk": "SSD",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "South Sudan"
                        }
                    },
                    {
                        "pk": "TUN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Tunisia"
                        }
                    },
                    {
                        "pk": "TGO",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Togo"
                        }
                    },
                    {
                        "pk": "ZAF",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "South Africa"
                        }
                    },
                    {
                        "pk": "NER",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Niger"
                        }
                    },
                    {
                        "pk": "MAR",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Morocco"
                        }
                    },
                    {
                        "pk": "KEN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Kenya"
                        }
                    },
                    {
                        "pk": "SYC",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Seychelles"
                        }
                    },
                    {
                        "pk": "ESH",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Western Sahara"
                        }
                    },
                    {
                        "pk": "SHN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Saint Helena"
                        }
                    },
                    {
                        "pk": "REU",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Reunion"
                        }
                    },
                    {
                        "pk": "SLE",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Sierra Leone"
                        }
                    },
                    {
                        "pk": "DJI",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Djibouti"
                        }
                    },
                    {
                        "pk": "STP",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Sao Tome and Principe"
                        }
                    },
                    {
                        "pk": "BDI",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Burundi"
                        }
                    },
                    {
                        "pk": "MLI",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Mali"
                        }
                    },
                    {
                        "pk": "GMB",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Gambia"
                        }
                    },
                    {
                        "pk": "ZMB",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Zambia"
                        }
                    },
                    {
                        "pk": "RWA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Rwanda"
                        }
                    },
                    {
                        "pk": "MWI",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Malawi"
                        }
                    },
                    {
                        "pk": "BFA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Burkina Faso"
                        }
                    },
                    {
                        "pk": "GAB",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Gabon"
                        }
                    },
                    {
                        "pk": "TCD",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Chad"
                        }
                    },
                    {
                        "pk": "SEN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Senegal"
                        }
                    },
                    {
                        "pk": "NAM",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Namibia"
                        }
                    },
                    {
                        "pk": "ATF",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "French Southern Territories"
                        }
                    },
                    {
                        "pk": "BWA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Botswana"
                        }
                    },
                    {
                        "pk": "TZA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Tanzania"
                        }
                    },
                    {
                        "pk": "ERI",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Eritrea"
                        }
                    },
                    {
                        "pk": "MYT",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Mayotte"
                        }
                    },
                    {
                        "pk": "SOM",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Somalia"
                        }
                    },
                    {
                        "pk": "UGA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Uganda"
                        }
                    },
                    {
                        "pk": "LSO",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Lesotho"
                        }
                    },
                    {
                        "pk": "LBY",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Libya"
                        }
                    },
                    {
                        "pk": "LBR",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Liberia"
                        }
                    },
                    {
                        "pk": "COD",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Democratic Republic of the Congo"
                        }
                    },
                    {
                        "pk": "CPV",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Cape Verde"
                        }
                    },
                    {
                        "pk": "CAF",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Central African Republic"
                        }
                    },
                    {
                        "pk": "COM",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Comoros"
                        }
                    },
                    {
                        "pk": "ETH",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Ethiopia"
                        }
                    },
                    {
                        "pk": "GIN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Guinea"
                        }
                    },
                    {
                        "pk": "COG",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Republic of Congo"
                        }
                    },
                    {
                        "pk": "GHA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Ghana"
                        }
                    },
                    {
                        "pk": "SWZ",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Swaziland"
                        }
                    },
                    {
                        "pk": "GNB",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Guinea-Bissau"
                        }
                    },
                    {
                        "pk": "MDG",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Madagascar"
                        }
                    },
                    {
                        "pk": "ZWE",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Zimbabwe"
                        }
                    },
                    {
                        "pk": "MOZ",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Mozambique"
                        }
                    },
                    {
                        "pk": "MRT",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Mauritania"
                        }
                    },
                    {
                        "pk": "MUS",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Mauritius"
                        }
                    },
                    {
                        "pk": "NGA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Nigeria"
                        }
                    },
                    {
                        "pk": "BEN",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Benin"
                        }
                    },
                    {
                        "pk": "GNQ",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Equatorial Guinea"
                        }
                    },
                    {
                        "pk": "EGY",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Egypt"
                        }
                    },
                    {
                        "pk": "AGO",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Angola"
                        }
                    },
                    {
                        "pk": "DZA",
                        "model": "vulnerability.country",
                        "fields": {
                            "is_visible": true,
                            "region": 2,
                            "name": "Algeria"
                        }
                    }
                ],
                "lon": null,
                "address": "",
                "lat": null
            }
        },
        "authors": "Martins and Silva",
        "use_case_information": "",
        "structure_type": "Building",
        "taxonomy_gem": "DX+D99/MAT99/L99/DY+D99/MAT99/L99/H99/Y99/OC99/BP99/PLF99/IR99/EW99/RSH99+RMT99+R99+RWC99/F99+FWC99/FOS99",
        "year": 2018,
        "type_of_assessment": "Vulnerability",
        "taxonomy_type": {
            "pk": 1,
            "model": "vulnerability.taxonomytype",
            "fields": {
                "user_def": false,
                "name": "GEM"
            }
        },
        "taxonomy_text": "",
        "vulnerability_func": {
            "pk": 30,
            "model": "vulnerability.vulnerabilityfunc",
            "fields": {
                "general_information": 670,
                "predictor_var": {
                    "pk": 602,
                    "model": "vulnerability.predictorvar",
                    "fields": {
                        "type_of_period": "Telastic (s)",
                        "minimum_im": 0.05,
                        "intensity_measure_type": "Sa(T)",
                        "fragility_func": null,
                        "period": 0.3,
                        "vulnerability_func": 30,
                        "maximum_im": 8,
                        "intensity_measure_unit": "g"
                    }
                },
                "func_distr_vuln_discr": {
                    "pk": 23,
                    "model": "vulnerability.funcdistrvulndiscr",
                    "fields": {
                        "resp_var_mean_val": "0.000025;0.000033;0.000045;0.000059;0.000078;0.000101;0.000131;0.00017;0.000217;0.000277;0.00035;0.00044;0.000551;0.000684;0.000844;0.001036;0.001264;0.001532;0.001846;0.002212;0.002635;0.003121;0.003676;0.004306;0.005016;0.005812;0.006701;0.007685;0.008772;0.009966;0.01127;0.012691;0.014233;0.0159;0.0177;0.019639;0.021726;0.023972;0.026388;0.028991;0.0318;0.034838;0.038132;0.041714;0.04562;0.049893;0.05458;0.059733;0.065409;0.07167;0.078582;0.086213;0.094633;0.103913;0.114125;0.125335;0.13761;0.151007;0.165578;0.181366;0.1984;0.216699;0.236266;0.257091;0.279144;0.302379;0.326733;0.352124;0.378455;0.40561;0.433459;0.461862;0.490663;0.519703;0.548813;0.577824;0.606566;0.634874;0.662589;0.68956;0.715649;0.740732;0.764702;0.787467;0.808956;0.829114;0.847907;0.865317;0.881347;0.896012;0.909344;0.921388;0.932199;0.941842;0.950388;0.957913;0.964497;0.97022;0.975163;0.979405",
                        "resp_var_val_coeff": "",
                        "predictor_var_im_val": "0.05;0.05263;0.055398;0.058312;0.06138;0.064608;0.068007;0.071584;0.07535;0.079313;0.083485;0.087876;0.092499;0.097364;0.102486;0.107877;0.113551;0.119524;0.125811;0.132429;0.139395;0.146727;0.154445;0.162569;0.171121;0.180122;0.189596;0.199569;0.210067;0.221117;0.232748;0.24499;0.257877;0.271442;0.28572;0.300749;0.316569;0.333221;0.350749;0.369198;0.388619;0.40906;0.430577;0.453226;0.477066;0.502161;0.528575;0.556379;0.585645;0.61645;0.648876;0.683008;0.718935;0.756752;0.796558;0.838458;0.882561;0.928985;0.977851;1.029287;1.083429;1.140418;1.200405;1.263548;1.330012;1.399972;1.473613;1.551126;1.632717;1.7186;1.809;1.904156;2.004317;2.109746;2.220721;2.337534;2.460491;2.589915;2.726148;2.869546;3.020488;3.179369;3.346607;3.522643;3.707938;3.902979;4.108281;4.324381;4.551848;4.791281;5.043307;5.308591;5.587829;5.881755;6.191142;6.516804;6.859595;7.220418;7.60022;8",
                        "vulnerability_func": 30,
                        "func_distr_shape": null,
                        "data_pts_num": 100
                    }
                },
                "method_of_estimation": "Analytical",
                "func_distr_type": "Discrete",
                "resp_var": "Damage factor"
            }
        }
    }
}

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save multiple values of a variable in a json file in R

Eric Berger
I have had success using the fromJSON() function in the rjson package.

On Thu, Jan 16, 2020 at 4:22 PM Ioanna Ioannou <[hidden email]> wrote:

> hello everyone,
>
> and happy new year!
>
> I have this problem: I want to save the name of the 'countries', the
> 'taxonomy_gem' and the 'minimum_im' and 'maximum_im' . The problem is that
> there are several names of countries.  How can i transfer the information
> from the json file to an R data.frame? See below for the json file.
>
> Best,
> ioanna
>
>
>
> {
>     "pk": 670,
>     "model": "vulnerability.generalinformation",
>     "fields": {
>         "category": "Structure class",
>         "article_title": "A GLOBAL DATABASE OF VULNERABILITY MODELS FOR
> SEISMIC RISK ASSESSMENT",
>         "name": "CR/LFINF/DUL/H:2",
>         "publication_conference_name": "16EECE",
>         "llrs": null,
>         "material": null,
>         "web_link": "",
>         "owner": {
>             "pk": 1900,
>             "model": "people.profile",
>             "fields": {
>                 "username": "lmartins",
>                 "first_name": "",
>                 "last_name": "",
>                 "email": "[hidden email]"
>             }
>         },
>         "general_comments": "",
>         "geo_applicability": {
>             "pk": 669,
>             "model": "vulnerability.geoapplicability",
>             "fields": {
>                 "general_information": 670,
>                 "area": "",
>                 "countries": [
>                     {
>                         "pk": "CIV",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Cte d'Ivoire"
>                         }
>                     },
>                     {
>                         "pk": "CMR",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Cameroon"
>                         }
>                     },
>                     {
>                         "pk": "SDN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Sudan"
>                         }
>                     },
>                     {
>                         "pk": "SSD",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "South Sudan"
>                         }
>                     },
>                     {
>                         "pk": "TUN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Tunisia"
>                         }
>                     },
>                     {
>                         "pk": "TGO",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Togo"
>                         }
>                     },
>                     {
>                         "pk": "ZAF",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "South Africa"
>                         }
>                     },
>                     {
>                         "pk": "NER",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Niger"
>                         }
>                     },
>                     {
>                         "pk": "MAR",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Morocco"
>                         }
>                     },
>                     {
>                         "pk": "KEN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Kenya"
>                         }
>                     },
>                     {
>                         "pk": "SYC",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Seychelles"
>                         }
>                     },
>                     {
>                         "pk": "ESH",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Western Sahara"
>                         }
>                     },
>                     {
>                         "pk": "SHN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Saint Helena"
>                         }
>                     },
>                     {
>                         "pk": "REU",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Reunion"
>                         }
>                     },
>                     {
>                         "pk": "SLE",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Sierra Leone"
>                         }
>                     },
>                     {
>                         "pk": "DJI",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Djibouti"
>                         }
>                     },
>                     {
>                         "pk": "STP",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Sao Tome and Principe"
>                         }
>                     },
>                     {
>                         "pk": "BDI",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Burundi"
>                         }
>                     },
>                     {
>                         "pk": "MLI",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Mali"
>                         }
>                     },
>                     {
>                         "pk": "GMB",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Gambia"
>                         }
>                     },
>                     {
>                         "pk": "ZMB",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Zambia"
>                         }
>                     },
>                     {
>                         "pk": "RWA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Rwanda"
>                         }
>                     },
>                     {
>                         "pk": "MWI",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Malawi"
>                         }
>                     },
>                     {
>                         "pk": "BFA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Burkina Faso"
>                         }
>                     },
>                     {
>                         "pk": "GAB",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Gabon"
>                         }
>                     },
>                     {
>                         "pk": "TCD",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Chad"
>                         }
>                     },
>                     {
>                         "pk": "SEN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Senegal"
>                         }
>                     },
>                     {
>                         "pk": "NAM",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Namibia"
>                         }
>                     },
>                     {
>                         "pk": "ATF",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "French Southern Territories"
>                         }
>                     },
>                     {
>                         "pk": "BWA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Botswana"
>                         }
>                     },
>                     {
>                         "pk": "TZA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Tanzania"
>                         }
>                     },
>                     {
>                         "pk": "ERI",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Eritrea"
>                         }
>                     },
>                     {
>                         "pk": "MYT",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Mayotte"
>                         }
>                     },
>                     {
>                         "pk": "SOM",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Somalia"
>                         }
>                     },
>                     {
>                         "pk": "UGA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Uganda"
>                         }
>                     },
>                     {
>                         "pk": "LSO",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Lesotho"
>                         }
>                     },
>                     {
>                         "pk": "LBY",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Libya"
>                         }
>                     },
>                     {
>                         "pk": "LBR",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Liberia"
>                         }
>                     },
>                     {
>                         "pk": "COD",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Democratic Republic of the Congo"
>                         }
>                     },
>                     {
>                         "pk": "CPV",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Cape Verde"
>                         }
>                     },
>                     {
>                         "pk": "CAF",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Central African Republic"
>                         }
>                     },
>                     {
>                         "pk": "COM",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Comoros"
>                         }
>                     },
>                     {
>                         "pk": "ETH",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Ethiopia"
>                         }
>                     },
>                     {
>                         "pk": "GIN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Guinea"
>                         }
>                     },
>                     {
>                         "pk": "COG",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Republic of Congo"
>                         }
>                     },
>                     {
>                         "pk": "GHA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Ghana"
>                         }
>                     },
>                     {
>                         "pk": "SWZ",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Swaziland"
>                         }
>                     },
>                     {
>                         "pk": "GNB",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Guinea-Bissau"
>                         }
>                     },
>                     {
>                         "pk": "MDG",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Madagascar"
>                         }
>                     },
>                     {
>                         "pk": "ZWE",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Zimbabwe"
>                         }
>                     },
>                     {
>                         "pk": "MOZ",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Mozambique"
>                         }
>                     },
>                     {
>                         "pk": "MRT",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Mauritania"
>                         }
>                     },
>                     {
>                         "pk": "MUS",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Mauritius"
>                         }
>                     },
>                     {
>                         "pk": "NGA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Nigeria"
>                         }
>                     },
>                     {
>                         "pk": "BEN",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Benin"
>                         }
>                     },
>                     {
>                         "pk": "GNQ",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Equatorial Guinea"
>                         }
>                     },
>                     {
>                         "pk": "EGY",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Egypt"
>                         }
>                     },
>                     {
>                         "pk": "AGO",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Angola"
>                         }
>                     },
>                     {
>                         "pk": "DZA",
>                         "model": "vulnerability.country",
>                         "fields": {
>                             "is_visible": true,
>                             "region": 2,
>                             "name": "Algeria"
>                         }
>                     }
>                 ],
>                 "lon": null,
>                 "address": "",
>                 "lat": null
>             }
>         },
>         "authors": "Martins and Silva",
>         "use_case_information": "",
>         "structure_type": "Building",
>         "taxonomy_gem":
> "DX+D99/MAT99/L99/DY+D99/MAT99/L99/H99/Y99/OC99/BP99/PLF99/IR99/EW99/RSH99+RMT99+R99+RWC99/F99+FWC99/FOS99",
>         "year": 2018,
>         "type_of_assessment": "Vulnerability",
>         "taxonomy_type": {
>             "pk": 1,
>             "model": "vulnerability.taxonomytype",
>             "fields": {
>                 "user_def": false,
>                 "name": "GEM"
>             }
>         },
>         "taxonomy_text": "",
>         "vulnerability_func": {
>             "pk": 30,
>             "model": "vulnerability.vulnerabilityfunc",
>             "fields": {
>                 "general_information": 670,
>                 "predictor_var": {
>                     "pk": 602,
>                     "model": "vulnerability.predictorvar",
>                     "fields": {
>                         "type_of_period": "Telastic (s)",
>                         "minimum_im": 0.05,
>                         "intensity_measure_type": "Sa(T)",
>                         "fragility_func": null,
>                         "period": 0.3,
>                         "vulnerability_func": 30,
>                         "maximum_im": 8,
>                         "intensity_measure_unit": "g"
>                     }
>                 },
>                 "func_distr_vuln_discr": {
>                     "pk": 23,
>                     "model": "vulnerability.funcdistrvulndiscr",
>                     "fields": {
>                         "resp_var_mean_val":
> "0.000025;0.000033;0.000045;0.000059;0.000078;0.000101;0.000131;0.00017;0.000217;0.000277;0.00035;0.00044;0.000551;0.000684;0.000844;0.001036;0.001264;0.001532;0.001846;0.002212;0.002635;0.003121;0.003676;0.004306;0.005016;0.005812;0.006701;0.007685;0.008772;0.009966;0.01127;0.012691;0.014233;0.0159;0.0177;0.019639;0.021726;0.023972;0.026388;0.028991;0.0318;0.034838;0.038132;0.041714;0.04562;0.049893;0.05458;0.059733;0.065409;0.07167;0.078582;0.086213;0.094633;0.103913;0.114125;0.125335;0.13761;0.151007;0.165578;0.181366;0.1984;0.216699;0.236266;0.257091;0.279144;0.302379;0.326733;0.352124;0.378455;0.40561;0.433459;0.461862;0.490663;0.519703;0.548813;0.577824;0.606566;0.634874;0.662589;0.68956;0.715649;0.740732;0.764702;0.787467;0.808956;0.829114;0.847907;0.865317;0.881347;0.896012;0.909344;0.921388;0.932199;0.941842;0.950388;0.957913;0.964497;0.97022;0.975163;0.979405",
>                         "resp_var_val_coeff": "",
>                         "predictor_var_im_val":
> "0.05;0.05263;0.055398;0.058312;0.06138;0.064608;0.068007;0.071584;0.07535;0.079313;0.083485;0.087876;0.092499;0.097364;0.102486;0.107877;0.113551;0.119524;0.125811;0.132429;0.139395;0.146727;0.154445;0.162569;0.171121;0.180122;0.189596;0.199569;0.210067;0.221117;0.232748;0.24499;0.257877;0.271442;0.28572;0.300749;0.316569;0.333221;0.350749;0.369198;0.388619;0.40906;0.430577;0.453226;0.477066;0.502161;0.528575;0.556379;0.585645;0.61645;0.648876;0.683008;0.718935;0.756752;0.796558;0.838458;0.882561;0.928985;0.977851;1.029287;1.083429;1.140418;1.200405;1.263548;1.330012;1.399972;1.473613;1.551126;1.632717;1.7186;1.809;1.904156;2.004317;2.109746;2.220721;2.337534;2.460491;2.589915;2.726148;2.869546;3.020488;3.179369;3.346607;3.522643;3.707938;3.902979;4.108281;4.324381;4.551848;4.791281;5.043307;5.308591;5.587829;5.881755;6.191142;6.516804;6.859595;7.220418;7.60022;8",
>                         "vulnerability_func": 30,
>                         "func_distr_shape": null,
>                         "data_pts_num": 100
>                     }
>                 },
>                 "method_of_estimation": "Analytical",
>                 "func_distr_type": "Discrete",
>                 "resp_var": "Damage factor"
>             }
>         }
>     }
> }
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save multiple values of a variable in a json file in R

ioanna
In reply to this post by ioanna
ok, my problem is the follwoing
a<- read_json(json_file)
a$fields$geo_applicability$fields$countries[[1]]$fields$name

this was i can see the name of a single country reported first in the json. HOwever, i  need to save all 59. My worry is that i have multiple files that i need to read and in some cases there will be one country reported and in some others multiple. How can i optimise the code?

Best,

________________________________
From: Rainer M Krug <[hidden email]>
Sent: 16 January 2020 14:47
To: Ioanna Ioannou <[hidden email]>
Subject: Re: [R] How to save multiple values of a variable in a json file in R

Check the hep of the read_json() function and please keep the conversation on the list

On 16 Jan 2020, at 15:42, Ioanna Ioannou <[hidden email]<mailto:[hidden email]>> wrote:

I will try the package but i could do with a worked example.

________________________________
From: Rainer M Krug <[hidden email]<mailto:[hidden email]>>
Sent: 16 January 2020 14:29
To: Ioanna Ioannou <[hidden email]<mailto:[hidden email]>>
Subject: Re: [R] How to save multiple values of a variable in a json file in R

Have you looked at the jsonlite package?

Rainer


On 16 Jan 2020, at 15:21, Ioanna Ioannou <[hidden email]<mailto:[hidden email]>> wrote:

hello everyone,

and happy new year!

I have this problem: I want to save the name of the 'countries', the 'taxonomy_gem' and the 'minimum_im' and 'maximum_im' . The problem is that there are several names of countries.  How can i transfer the information from the json file to an R data.frame? See below for the json file.

Best,
ioanna



json_file<- {
   "pk": 670,
   "model": "vulnerability.generalinformation",
   "fields": {
       "category": "Structure class",
       "article_title": "A GLOBAL DATABASE OF VULNERABILITY MODELS FOR SEISMIC RISK ASSESSMENT",
       "name": "CR/LFINF/DUL/H:2",
       "publication_conference_name": "16EECE",
       "llrs": null,
       "material": null,
       "web_link": "",
       "owner": {
           "pk": 1900,
           "model": "people.profile",
           "fields": {
               "username": "lmartins",
               "first_name": "",
               "last_name": "",
               "email": "[hidden email]<mailto:[hidden email]>"
           }
       },
       "general_comments": "",
       "geo_applicability": {
           "pk": 669,
           "model": "vulnerability.geoapplicability",
           "fields": {
               "general_information": 670,
               "area": "",
               "countries": [
                   {
                       "pk": "CIV",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Cte d'Ivoire"
                       }
                   },
                   {
                       "pk": "CMR",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Cameroon"
                       }
                   },
                   {
                       "pk": "SDN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Sudan"
                       }
                   },
                   {
                       "pk": "SSD",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "South Sudan"
                       }
                   },
                   {
                       "pk": "TUN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Tunisia"
                       }
                   },
                   {
                       "pk": "TGO",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Togo"
                       }
                   },
                   {
                       "pk": "ZAF",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "South Africa"
                       }
                   },
                   {
                       "pk": "NER",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Niger"
                       }
                   },
                   {
                       "pk": "MAR",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Morocco"
                       }
                   },
                   {
                       "pk": "KEN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Kenya"
                       }
                   },
                   {
                       "pk": "SYC",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Seychelles"
                       }
                   },
                   {
                       "pk": "ESH",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Western Sahara"
                       }
                   },
                   {
                       "pk": "SHN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Saint Helena"
                       }
                   },
                   {
                       "pk": "REU",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Reunion"
                       }
                   },
                   {
                       "pk": "SLE",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Sierra Leone"
                       }
                   },
                   {
                       "pk": "DJI",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Djibouti"
                       }
                   },
                   {
                       "pk": "STP",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Sao Tome and Principe"
                       }
                   },
                   {
                       "pk": "BDI",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Burundi"
                       }
                   },
                   {
                       "pk": "MLI",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Mali"
                       }
                   },
                   {
                       "pk": "GMB",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Gambia"
                       }
                   },
                   {
                       "pk": "ZMB",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Zambia"
                       }
                   },
                   {
                       "pk": "RWA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Rwanda"
                       }
                   },
                   {
                       "pk": "MWI",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Malawi"
                       }
                   },
                   {
                       "pk": "BFA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Burkina Faso"
                       }
                   },
                   {
                       "pk": "GAB",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Gabon"
                       }
                   },
                   {
                       "pk": "TCD",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Chad"
                       }
                   },
                   {
                       "pk": "SEN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Senegal"
                       }
                   },
                   {
                       "pk": "NAM",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Namibia"
                       }
                   },
                   {
                       "pk": "ATF",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "French Southern Territories"
                       }
                   },
                   {
                       "pk": "BWA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Botswana"
                       }
                   },
                   {
                       "pk": "TZA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Tanzania"
                       }
                   },
                   {
                       "pk": "ERI",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Eritrea"
                       }
                   },
                   {
                       "pk": "MYT",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Mayotte"
                       }
                   },
                   {
                       "pk": "SOM",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Somalia"
                       }
                   },
                   {
                       "pk": "UGA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Uganda"
                       }
                   },
                   {
                       "pk": "LSO",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Lesotho"
                       }
                   },
                   {
                       "pk": "LBY",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Libya"
                       }
                   },
                   {
                       "pk": "LBR",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Liberia"
                       }
                   },
                   {
                       "pk": "COD",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Democratic Republic of the Congo"
                       }
                   },
                   {
                       "pk": "CPV",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Cape Verde"
                       }
                   },
                   {
                       "pk": "CAF",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Central African Republic"
                       }
                   },
                   {
                       "pk": "COM",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Comoros"
                       }
                   },
                   {
                       "pk": "ETH",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Ethiopia"
                       }
                   },
                   {
                       "pk": "GIN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Guinea"
                       }
                   },
                   {
                       "pk": "COG",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Republic of Congo"
                       }
                   },
                   {
                       "pk": "GHA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Ghana"
                       }
                   },
                   {
                       "pk": "SWZ",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Swaziland"
                       }
                   },
                   {
                       "pk": "GNB",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Guinea-Bissau"
                       }
                   },
                   {
                       "pk": "MDG",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Madagascar"
                       }
                   },
                   {
                       "pk": "ZWE",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Zimbabwe"
                       }
                   },
                   {
                       "pk": "MOZ",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Mozambique"
                       }
                   },
                   {
                       "pk": "MRT",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Mauritania"
                       }
                   },
                   {
                       "pk": "MUS",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Mauritius"
                       }
                   },
                   {
                       "pk": "NGA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Nigeria"
                       }
                   },
                   {
                       "pk": "BEN",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Benin"
                       }
                   },
                   {
                       "pk": "GNQ",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Equatorial Guinea"
                       }
                   },
                   {
                       "pk": "EGY",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Egypt"
                       }
                   },
                   {
                       "pk": "AGO",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Angola"
                       }
                   },
                   {
                       "pk": "DZA",
                       "model": "vulnerability.country",
                       "fields": {
                           "is_visible": true,
                           "region": 2,
                           "name": "Algeria"
                       }
                   }
               ],
               "lon": null,
               "address": "",
               "lat": null
           }
       },
       "authors": "Martins and Silva",
       "use_case_information": "",
       "structure_type": "Building",
       "taxonomy_gem": "DX+D99/MAT99/L99/DY+D99/MAT99/L99/H99/Y99/OC99/BP99/PLF99/IR99/EW99/RSH99+RMT99+R99+RWC99/F99+FWC99/FOS99",
       "year": 2018,
       "type_of_assessment": "Vulnerability",
       "taxonomy_type": {
           "pk": 1,
           "model": "vulnerability.taxonomytype",
           "fields": {
               "user_def": false,
               "name": "GEM"
           }
       },
       "taxonomy_text": "",
       "vulnerability_func": {
           "pk": 30,
           "model": "vulnerability.vulnerabilityfunc",
           "fields": {
               "general_information": 670,
               "predictor_var": {
                   "pk": 602,
                   "model": "vulnerability.predictorvar",
                   "fields": {
                       "type_of_period": "Telastic (s)",
                       "minimum_im": 0.05,
                       "intensity_measure_type": "Sa(T)",
                       "fragility_func": null,
                       "period": 0.3,
                       "vulnerability_func": 30,
                       "maximum_im": 8,
                       "intensity_measure_unit": "g"
                   }
               },
               "func_distr_vuln_discr": {
                   "pk": 23,
                   "model": "vulnerability.funcdistrvulndiscr",
                   "fields": {
                       "resp_var_mean_val": "0.000025;0.000033;0.000045;0.000059;0.000078;0.000101;0.000131;0.00017;0.000217;0.000277;0.00035;0.00044;0.000551;0.000684;0.000844;0.001036;0.001264;0.001532;0.001846;0.002212;0.002635;0.003121;0.003676;0.004306;0.005016;0.005812;0.006701;0.007685;0.008772;0.009966;0.01127;0.012691;0.014233;0.0159;0.0177;0.019639;0.021726;0.023972;0.026388;0.028991;0.0318;0.034838;0.038132;0.041714;0.04562;0.049893;0.05458;0.059733;0.065409;0.07167;0.078582;0.086213;0.094633;0.103913;0.114125;0.125335;0.13761;0.151007;0.165578;0.181366;0.1984;0.216699;0.236266;0.257091;0.279144;0.302379;0.326733;0.352124;0.378455;0.40561;0.433459;0.461862;0.490663;0.519703;0.548813;0.577824;0.606566;0.634874;0.662589;0.68956;0.715649;0.740732;0.764702;0.787467;0.808956;0.829114;0.847907;0.865317;0.881347;0.896012;0.909344;0.921388;0.932199;0.941842;0.950388;0.957913;0.964497;0.97022;0.975163;0.979405",
                       "resp_var_val_coeff": "",
                       "predictor_var_im_val": "0.05;0.05263;0.055398;0.058312;0.06138;0.064608;0.068007;0.071584;0.07535;0.079313;0.083485;0.087876;0.092499;0.097364;0.102486;0.107877;0.113551;0.119524;0.125811;0.132429;0.139395;0.146727;0.154445;0.162569;0.171121;0.180122;0.189596;0.199569;0.210067;0.221117;0.232748;0.24499;0.257877;0.271442;0.28572;0.300749;0.316569;0.333221;0.350749;0.369198;0.388619;0.40906;0.430577;0.453226;0.477066;0.502161;0.528575;0.556379;0.585645;0.61645;0.648876;0.683008;0.718935;0.756752;0.796558;0.838458;0.882561;0.928985;0.977851;1.029287;1.083429;1.140418;1.200405;1.263548;1.330012;1.399972;1.473613;1.551126;1.632717;1.7186;1.809;1.904156;2.004317;2.109746;2.220721;2.337534;2.460491;2.589915;2.726148;2.869546;3.020488;3.179369;3.346607;3.522643;3.707938;3.902979;4.108281;4.324381;4.551848;4.791281;5.043307;5.308591;5.587829;5.881755;6.191142;6.516804;6.859595;7.220418;7.60022;8",
                       "vulnerability_func": 30,
                       "func_distr_shape": null,
                       "data_pts_num": 100
                   }
               },
               "method_of_estimation": "Analytical",
               "func_distr_type": "Discrete",
               "resp_var": "Damage factor"
           }
       }
   }
}

[[alternative HTML version deleted]]

______________________________________________
[hidden email]<mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Orcid ID: 0000-0002-7490-0066

Department of Evolutionary Biology and Environmental Studies
University of Z�rich
Office Y34-J-74
Winterthurerstrasse 190
8075 Z�rich
Switzerland

Office: +41 (0)44 635 47 64
Cell:        +41 (0)78 630 66 57
email:      [hidden email]<mailto:[hidden email]>
[hidden email]<mailto:[hidden email]>
Skype:     RMkrug

PGP: 0x0F52F982

--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Orcid ID: 0000-0002-7490-0066

Department of Evolutionary Biology and Environmental Studies
University of Z�rich
Office Y34-J-74
Winterthurerstrasse 190
8075 Z�rich
Switzerland

Office: +41 (0)44 635 47 64
Cell:        +41 (0)78 630 66 57
email:      [hidden email]<mailto:[hidden email]>
[hidden email]<mailto:[hidden email]>
Skype:     RMkrug

PGP: 0x0F52F982




        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save multiple values of a variable in a json file in R

Jim Lemon-4

Hi Ioanna,
Assuming your input file is named "ii.json" and contains the text in
your original post:

library(jsonlite)
a2<-fromJSON("ii.json",flatten=TRUE)
a2$fields$geo_applicability$fields$countries[,"fields.name"]

You can find this yourself by progressively checking names and
extracting until you reach the data frame that contains the
information you want. Note that the format of your files must be the
same for this to work on more than one JSON file.

Jim

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save multiple values of a variable in a json file in R

Richard O'Keefe-2
In reply to this post by ioanna

In your example, these keys occur
   1  countries
   1  maximum_im
   1  minimum_im
 61  name
 66  pk           -- many of these pertain to countries, some do not
   1  taxonomy_gem
It is not clear what you mean by 'the name of the countries'.
There appear to be 61 countries each with two 'names'.
I am guessing that you want the 'name' slots, not the locale-
independent 3-letter ISO codes.
So 'name' occurs once per country.
'maximum_im' and 'minimum_im' occur once per predictor_var?
And 'taxonomy_gem' appears to occur once per dataset.
Now in a data frame, every variable occurs the same number of times
(once per row = observation = case = thingy).
So, leaving aside the issue of parsing JSON, suppose someone
gave you
 - a vector of N country Names
 - a vector of P maximum_im values for Predictors
 - a vector of P minimum_im values for Predictors
 - a vector of T taxonomy_gems
How do you intend to fit these into a data frame?

On Fri, 17 Jan 2020 at 04:03, Ioanna Ioannou <[hidden email]> wrote:

>
> ok, my problem is the follwoing
> a<- read_json(json_file)
> a$fields$geo_applicability$fields$countries[[1]]$fields$name
>
> this was i can see the name of a single country reported first in the json. HOwever, i  need to save all 59. My worry is that i have multiple files that i need to read and in some cases there will be one country reported and in some others multiple. How can i optimise the code?
>
> Best,
>
> ________________________________
> From: Rainer M Krug <[hidden email]>
> Sent: 16 January 2020 14:47
> To: Ioanna Ioannou <[hidden email]>
> Subject: Re: [R] How to save multiple values of a variable in a json file in R
>
> Check the hep of the read_json() function and please keep the conversation on the list
>
> On 16 Jan 2020, at 15:42, Ioanna Ioannou <[hidden email]<mailto:[hidden email]>> wrote:
>
> I will try the package but i could do with a worked example.
>
> ________________________________
> From: Rainer M Krug <[hidden email]<mailto:[hidden email]>>
> Sent: 16 January 2020 14:29
> To: Ioanna Ioannou <[hidden email]<mailto:[hidden email]>>
> Subject: Re: [R] How to save multiple values of a variable in a json file in R
>
> Have you looked at the jsonlite package?
>
> Rainer
>
>
> On 16 Jan 2020, at 15:21, Ioanna Ioannou <[hidden email]<mailto:[hidden email]>> wrote:
>
> hello everyone,
>
> and happy new year!
>
> I have this problem: I want to save the name of the 'countries', the 'taxonomy_gem' and the 'minimum_im' and 'maximum_im' . The problem is that there are several names of countries.  How can i transfer the information from the json file to an R data.frame? See below for the json file.
>
> Best,
> ioanna
>
>
>
> json_file<- {
>    "pk": 670,
>    "model": "vulnerability.generalinformation",
>    "fields": {
>        "category": "Structure class",
>        "article_title": "A GLOBAL DATABASE OF VULNERABILITY MODELS FOR SEISMIC RISK ASSESSMENT",
>        "name": "CR/LFINF/DUL/H:2",
>        "publication_conference_name": "16EECE",
>        "llrs": null,
>        "material": null,
>        "web_link": "",
>        "owner": {
>            "pk": 1900,
>            "model": "people.profile",
>            "fields": {
>                "username": "lmartins",
>                "first_name": "",
>                "last_name": "",
>                "email": "[hidden email]<mailto:[hidden email]>"
>            }
>        },
>        "general_comments": "",
>        "geo_applicability": {
>            "pk": 669,
>            "model": "vulnerability.geoapplicability",
>            "fields": {
>                "general_information": 670,
>                "area": "",
>                "countries": [
>                    {
>                        "pk": "CIV",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Cte d'Ivoire"
>                        }
>                    },
>                    {
>                        "pk": "CMR",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Cameroon"
>                        }
>                    },
>                    {
>                        "pk": "SDN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Sudan"
>                        }
>                    },
>                    {
>                        "pk": "SSD",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "South Sudan"
>                        }
>                    },
>                    {
>                        "pk": "TUN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Tunisia"
>                        }
>                    },
>                    {
>                        "pk": "TGO",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Togo"
>                        }
>                    },
>                    {
>                        "pk": "ZAF",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "South Africa"
>                        }
>                    },
>                    {
>                        "pk": "NER",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Niger"
>                        }
>                    },
>                    {
>                        "pk": "MAR",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Morocco"
>                        }
>                    },
>                    {
>                        "pk": "KEN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Kenya"
>                        }
>                    },
>                    {
>                        "pk": "SYC",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Seychelles"
>                        }
>                    },
>                    {
>                        "pk": "ESH",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Western Sahara"
>                        }
>                    },
>                    {
>                        "pk": "SHN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Saint Helena"
>                        }
>                    },
>                    {
>                        "pk": "REU",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Reunion"
>                        }
>                    },
>                    {
>                        "pk": "SLE",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Sierra Leone"
>                        }
>                    },
>                    {
>                        "pk": "DJI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Djibouti"
>                        }
>                    },
>                    {
>                        "pk": "STP",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Sao Tome and Principe"
>                        }
>                    },
>                    {
>                        "pk": "BDI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Burundi"
>                        }
>                    },
>                    {
>                        "pk": "MLI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mali"
>                        }
>                    },
>                    {
>                        "pk": "GMB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Gambia"
>                        }
>                    },
>                    {
>                        "pk": "ZMB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Zambia"
>                        }
>                    },
>                    {
>                        "pk": "RWA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Rwanda"
>                        }
>                    },
>                    {
>                        "pk": "MWI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Malawi"
>                        }
>                    },
>                    {
>                        "pk": "BFA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Burkina Faso"
>                        }
>                    },
>                    {
>                        "pk": "GAB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Gabon"
>                        }
>                    },
>                    {
>                        "pk": "TCD",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Chad"
>                        }
>                    },
>                    {
>                        "pk": "SEN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Senegal"
>                        }
>                    },
>                    {
>                        "pk": "NAM",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Namibia"
>                        }
>                    },
>                    {
>                        "pk": "ATF",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "French Southern Territories"
>                        }
>                    },
>                    {
>                        "pk": "BWA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Botswana"
>                        }
>                    },
>                    {
>                        "pk": "TZA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Tanzania"
>                        }
>                    },
>                    {
>                        "pk": "ERI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Eritrea"
>                        }
>                    },
>                    {
>                        "pk": "MYT",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mayotte"
>                        }
>                    },
>                    {
>                        "pk": "SOM",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Somalia"
>                        }
>                    },
>                    {
>                        "pk": "UGA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Uganda"
>                        }
>                    },
>                    {
>                        "pk": "LSO",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Lesotho"
>                        }
>                    },
>                    {
>                        "pk": "LBY",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Libya"
>                        }
>                    },
>                    {
>                        "pk": "LBR",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Liberia"
>                        }
>                    },
>                    {
>                        "pk": "COD",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Democratic Republic of the Congo"
>                        }
>                    },
>                    {
>                        "pk": "CPV",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Cape Verde"
>                        }
>                    },
>                    {
>                        "pk": "CAF",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Central African Republic"
>                        }
>                    },
>                    {
>                        "pk": "COM",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Comoros"
>                        }
>                    },
>                    {
>                        "pk": "ETH",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Ethiopia"
>                        }
>                    },
>                    {
>                        "pk": "GIN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Guinea"
>                        }
>                    },
>                    {
>                        "pk": "COG",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Republic of Congo"
>                        }
>                    },
>                    {
>                        "pk": "GHA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Ghana"
>                        }
>                    },
>                    {
>                        "pk": "SWZ",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Swaziland"
>                        }
>                    },
>                    {
>                        "pk": "GNB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Guinea-Bissau"
>                        }
>                    },
>                    {
>                        "pk": "MDG",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Madagascar"
>                        }
>                    },
>                    {
>                        "pk": "ZWE",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Zimbabwe"
>                        }
>                    },
>                    {
>                        "pk": "MOZ",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mozambique"
>                        }
>                    },
>                    {
>                        "pk": "MRT",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mauritania"
>                        }
>                    },
>                    {
>                        "pk": "MUS",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mauritius"
>                        }
>                    },
>                    {
>                        "pk": "NGA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Nigeria"
>                        }
>                    },
>                    {
>                        "pk": "BEN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Benin"
>                        }
>                    },
>                    {
>                        "pk": "GNQ",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Equatorial Guinea"
>                        }
>                    },
>                    {
>                        "pk": "EGY",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Egypt"
>                        }
>                    },
>                    {
>                        "pk": "AGO",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Angola"
>                        }
>                    },
>                    {
>                        "pk": "DZA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Algeria"
>                        }
>                    }
>                ],
>                "lon": null,
>                "address": "",
>                "lat": null
>            }
>        },
>        "authors": "Martins and Silva",
>        "use_case_information": "",
>        "structure_type": "Building",
>        "taxonomy_gem": "DX+D99/MAT99/L99/DY+D99/MAT99/L99/H99/Y99/OC99/BP99/PLF99/IR99/EW99/RSH99+RMT99+R99+RWC99/F99+FWC99/FOS99",
>        "year": 2018,
>        "type_of_assessment": "Vulnerability",
>        "taxonomy_type": {
>            "pk": 1,
>            "model": "vulnerability.taxonomytype",
>            "fields": {
>                "user_def": false,
>                "name": "GEM"
>            }
>        },
>        "taxonomy_text": "",
>        "vulnerability_func": {
>            "pk": 30,
>            "model": "vulnerability.vulnerabilityfunc",
>            "fields": {
>                "general_information": 670,
>                "predictor_var": {
>                    "pk": 602,
>                    "model": "vulnerability.predictorvar",
>                    "fields": {
>                        "type_of_period": "Telastic (s)",
>                        "minimum_im": 0.05,
>                        "intensity_measure_type": "Sa(T)",
>                        "fragility_func": null,
>                        "period": 0.3,
>                        "vulnerability_func": 30,
>                        "maximum_im": 8,
>                        "intensity_measure_unit": "g"
>                    }
>                },
>                "func_distr_vuln_discr": {
>                    "pk": 23,
>                    "model": "vulnerability.funcdistrvulndiscr",
>                    "fields": {
>                        "resp_var_mean_val": "0.000025;0.000033;0.000045;0.000059;0.000078;0.000101;0.000131;0.00017;0.000217;0.000277;0.00035;0.00044;0.000551;0.000684;0.000844;0.001036;0.001264;0.001532;0.001846;0.002212;0.002635;0.003121;0.003676;0.004306;0.005016;0.005812;0.006701;0.007685;0.008772;0.009966;0.01127;0.012691;0.014233;0.0159;0.0177;0.019639;0.021726;0.023972;0.026388;0.028991;0.0318;0.034838;0.038132;0.041714;0.04562;0.049893;0.05458;0.059733;0.065409;0.07167;0.078582;0.086213;0.094633;0.103913;0.114125;0.125335;0.13761;0.151007;0.165578;0.181366;0.1984;0.216699;0.236266;0.257091;0.279144;0.302379;0.326733;0.352124;0.378455;0.40561;0.433459;0.461862;0.490663;0.519703;0.548813;0.577824;0.606566;0.634874;0.662589;0.68956;0.715649;0.740732;0.764702;0.787467;0.808956;0.829114;0.847907;0.865317;0.881347;0.896012;0.909344;0.921388;0.932199;0.941842;0.950388;0.957913;0.964497;0.97022;0.975163;0.979405",
>                        "resp_var_val_coeff": "",
>                        "predictor_var_im_val": "0.05;0.05263;0.055398;0.058312;0.06138;0.064608;0.068007;0.071584;0.07535;0.079313;0.083485;0.087876;0.092499;0.097364;0.102486;0.107877;0.113551;0.119524;0.125811;0.132429;0.139395;0.146727;0.154445;0.162569;0.171121;0.180122;0.189596;0.199569;0.210067;0.221117;0.232748;0.24499;0.257877;0.271442;0.28572;0.300749;0.316569;0.333221;0.350749;0.369198;0.388619;0.40906;0.430577;0.453226;0.477066;0.502161;0.528575;0.556379;0.585645;0.61645;0.648876;0.683008;0.718935;0.756752;0.796558;0.838458;0.882561;0.928985;0.977851;1.029287;1.083429;1.140418;1.200405;1.263548;1.330012;1.399972;1.473613;1.551126;1.632717;1.7186;1.809;1.904156;2.004317;2.109746;2.220721;2.337534;2.460491;2.589915;2.726148;2.869546;3.020488;3.179369;3.346607;3.522643;3.707938;3.902979;4.108281;4.324381;4.551848;4.791281;5.043307;5.308591;5.587829;5.881755;6.191142;6.516804;6.859595;7.220418;7.60022;8",
>                        "vulnerability_func": 30,
>                        "func_distr_shape": null,
>                        "data_pts_num": 100
>                    }
>                },
>                "method_of_estimation": "Analytical",
>                "func_distr_type": "Discrete",
>                "resp_var": "Damage factor"
>            }
>        }
>    }
> }
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)
>
> Orcid ID: 0000-0002-7490-0066
>
> Department of Evolutionary Biology and Environmental Studies
> University of Zürich
> Office Y34-J-74
> Winterthurerstrasse 190
> 8075 Zürich
> Switzerland
>
> Office: +41 (0)44 635 47 64
> Cell:        +41 (0)78 630 66 57
> email:      [hidden email]<mailto:[hidden email]>
> [hidden email]<mailto:[hidden email]>
> Skype:     RMkrug
>
> PGP: 0x0F52F982
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)
>
> Orcid ID: 0000-0002-7490-0066
>
> Department of Evolutionary Biology and Environmental Studies
> University of Zürich
> Office Y34-J-74
> Winterthurerstrasse 190
> 8075 Zürich
> Switzerland
>
> Office: +41 (0)44 635 47 64
> Cell:        +41 (0)78 630 66 57
> email:      [hidden email]<mailto:[hidden email]>
> [hidden email]<mailto:[hidden email]>
> Skype:     RMkrug
>
> PGP: 0x0F52F982
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

How to create a vector by searching information in multiple data.tables in r?

ioanna
In reply to this post by ioanna
Hello everyone,

Once again i am a bit stack. I have over 200 json files with information. I managed to manipulate them and their format is rather difficult as shown below. Unfortunately, not all these files contain the same fields. I want to extract e.g., the country from all these files. How can i add NA for the files for which the country is not mentioned?

Here is a reproducible example. Lets say i have two files, three files, two provide the country and the one does not.  essentially i want a vector called country which will look like this:

Country <- c('Colombia', 'Greece', NA)

Any help much appreciated!

Best,
ioanna


A<- data.frame( name1 = c('fields', 'fields', 'fields'),
                              name2= c('category', 'asset', 'country'),
                              value  = c('Structure Class', 'Building', 'Colombia')



B<- data.frame( name1 = c('fields', 'fields', 'fields'),
                              name2= c('category', 'asset', 'country'),
                              value  = c('Structure Class', 'Building', 'Greece')



C<- data.frame( name1 = c('fields', 'fields', 'fields'),
                              name2= c('category', 'asset', 'assessment'),
                              value  = c('Structure Class', 'Building', 'Fragility')



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to create a vector by searching information in multiple data.tables in r?

Ivan Krylov
On Fri, 31 Jan 2020 18:06:00 +0000
Ioanna Ioannou <[hidden email]> wrote:

> I want to extract e.g., the country from all these files. How can i
> add NA for the files for which the country is not mentioned?

I am starting from the beginning, since I don't know what you have
tried and where exactly you are stuck.

> A<- data.frame( name1 = c('fields', 'fields', 'fields'),
>                               name2= c('category', 'asset',
> 'country'), value  = c('Structure Class', 'Building', 'Colombia')

Given one such data frame, we can use logical vector subscripts to
extract the 'country' field. The following command returns a logical
vector:

A[, 'name2'] == 'country'
# [1] FALSE FALSE  TRUE

If we pass it to the subscript operator (type ?'[' in the R prompt for
more info), we can get the matching rows of the data frame:

subs <- A[, 'name2'] == 'country'
A[subs, ]
#    name1   name2    value
# 3 fields country Colombia

Okay, now we just need to choose the correct column:

A[subs, 'value']
# [1] Colombia
# Levels: Building Colombia Structure Class

What happens if there is no "country" row?

C[C[, 'name2'] == 'country', 'value']
# factor(0)
# Levels: Building Fragility Structure Class

We get a 0-length vector instead of the NA we want. The length()
function and the `if` control-flow construct should let us test for
0-length vectors (see ?length and ?'if'):

x <- C[C[,'name2'] == 'country','value']
if (length(x) == 1) x else NA
# [1] NA

Bonus question: what happens if there is more than one "country" line
in the data frame? What should happen instead?

See also:
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Index-vectors

Note that the "value" column is a factor (that's why we are getting
these "Levels:" when we print the vectors; see ?factor). You want a
character vector, so we will coerce the value to the desired type using
the as.character() function.

> essentially i want a vector called country which will look like this:
>
> Country <- c('Colombia', 'Greece', NA)

Once we have a procedure to deal with one data frame, we can apply it
to multiple data frames by putting the procedure into a function and
calling it on a list of data frames using one of the *apply functions
(see ?vapply):

# TODO: produce the list programmatically by calling the JSON reading
# function on a vector of filenames
dataframes <- list(A, B, C)
# perform an anonymous function on each of the data frames,
# return the result as a vector
sapply(dataframes, function(x) {
 country <- x[x[,'name2'] == 'country','value'] # look for "country" row
 # return the country as a string if found one row, NA otherwise
 if (length(country) == 1) as.character(country) else NA
})

I am pretty sure there are other ways to perform this operation, but I
find this one the easiest to explain.

--
Best regards,
Ivan

P.S.

> [[alternative HTML version deleted]]

Please post e-mails in plain text, not HTML. See
<http://www.R-project.org/posting-guide.html> for more info.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

How to save output of multiple loops in a matrix

ioanna
In reply to this post by ioanna
Hello everyone,

I am having this data.frame. For each row you have 26 values aggregated in a cell and separated by a comma. I want to do some calculations for all unique names and taxonomy which include the four different damage states. I can estimate the results but i am struggling to save them in a data.frame and assign next to them the unique combination of the name, taxonomy. Any help much appreciated.


d1 <- read.csv('test.csv')

D2L <- c(0, 2, 10, 50, 100)

VC_final <- array(NA, length(distinct(d1[,c(65,4,3)])$Name) )
VC       <- matrix(NA, length(distinct(d1[,c(65,4,3)])$Name),length(unlist(str_split(as.character(d1[1,]$Y_vals), pattern = ","))))

# get the rows for the four damage states
DS1_rows <- d1$Damage_State ==  unique(d1$Damage_State)[4]
DS2_rows <- d1$Damage_State ==  unique(d1$Damage_State)[3]
DS3_rows <- d1$Damage_State ==  unique(d1$Damage_State)[2]
DS4_rows <- d1$Damage_State ==  unique(d1$Damage_State)[1]

# step through all possible values of IM.type and Taxonomy and Name
#### This is true for this subset not generalibale needs to be checked first ##

for(IM in unique(d1$IM_type)) {
  for(Tax in unique(d1$Taxonomy)) {
    for(Name in unique(d1$Name)) {
      # get a logical vector of the rows to be use DS5 in this calculation
      calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name


      # check that there are any such rows in the DS5ata frame
      if(sum(calc_rows)) {
        cat(IM,Tax,Name,"\n")
        # if so, fill in the four values for these rows
        VC[calc_rows]  <- D2L[1] * (1- as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) ) +
          D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) -
                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ",")))) +
          D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ","))) -
                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ",")))) +
          D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ","))) -
                      as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))) +
          D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))
        print(VC[calc_rows] )
      }
    }
  }
}

  for(Tax in unique(d1$Taxonomy)) {
    for(Name in unique(d1$Name)) {
      # get a logical vector of the rows to be use DS5 in this calculation
      calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name


      # check that there are any such rows in the DS5ata frame
      if(sum(calc_rows)) {
        cat(IM,Tax,Name,"\n")
        # if so, fill in the four values for these rows
        VC[calc_rows]  <- D2L[1] * (1- as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) ) +
          D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) -
                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ",")))) +
          D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ","))) -
                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ",")))) +
          D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ","))) -
                      as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))) +
          D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))
        print(unique(VC ))
      }
    }
  }

Vul <- distinct(d1[,c(65,4,3)])

dim(VC) <- c(length(unlist(str_split(as.character(d1[1,]$Y_vals), pattern = ","))),length(distinct(d1[,c(65,4,3)])$Name))  ## (rows, cols)
VC
VC_t <- t(VC)
Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse = ',')))

Vul$Y_vals <- Vulnerability




Best,
ioanna










Name    Taxonomy        Damage_State    Y_vals
Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)  CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1// Slight  4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.999989939
Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)  CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//  Collapse       0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106
Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)  CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//  Extensive      5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531
Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)  CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1// Moderate        0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227
Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2      Collapse       0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2      Extensive      0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2     Moderate        0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2     Slight  0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Collapse       0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Extensive      0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Moderate        0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Slight  0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save output of multiple loops in a matrix

Jeff Newmiller
You have again posted using HTML  and the result is unreadable. Please post a reproducible example using dput instead of assuming we can read your formatted code or table.

On March 21, 2020 8:59:58 AM PDT, Ioanna Ioannou <[hidden email]> wrote:

>Hello everyone,
>
>I am having this data.frame. For each row you have 26 values aggregated
>in a cell and separated by a comma. I want to do some calculations for
>all unique names and taxonomy which include the four different damage
>states. I can estimate the results but i am struggling to save them in
>a data.frame and assign next to them the unique combination of the
>name, taxonomy. Any help much appreciated.
>
>
>d1 <- read.csv('test.csv')
>
>D2L <- c(0, 2, 10, 50, 100)
>
>VC_final <- array(NA, length(distinct(d1[,c(65,4,3)])$Name) )
>VC       <- matrix(NA,
>length(distinct(d1[,c(65,4,3)])$Name),length(unlist(str_split(as.character(d1[1,]$Y_vals),
>pattern = ","))))
>
># get the rows for the four damage states
>DS1_rows <- d1$Damage_State ==  unique(d1$Damage_State)[4]
>DS2_rows <- d1$Damage_State ==  unique(d1$Damage_State)[3]
>DS3_rows <- d1$Damage_State ==  unique(d1$Damage_State)[2]
>DS4_rows <- d1$Damage_State ==  unique(d1$Damage_State)[1]
>
># step through all possible values of IM.type and Taxonomy and Name
>#### This is true for this subset not generalibale needs to be checked
>first ##
>
>for(IM in unique(d1$IM_type)) {
>  for(Tax in unique(d1$Taxonomy)) {
>    for(Name in unique(d1$Name)) {
>   # get a logical vector of the rows to be use DS5 in this calculation
>   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name
>
>
>      # check that there are any such rows in the DS5ata frame
>      if(sum(calc_rows)) {
>        cat(IM,Tax,Name,"\n")
>        # if so, fill in the four values for these rows
>VC[calc_rows]  <- D2L[1] * (1-
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) ) +
>D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ",")))) +
>D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ",")))) +
>D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))) +
>D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))
>        print(VC[calc_rows] )
>      }
>    }
>  }
>}
>
>  for(Tax in unique(d1$Taxonomy)) {
>    for(Name in unique(d1$Name)) {
>   # get a logical vector of the rows to be use DS5 in this calculation
>   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name
>
>
>      # check that there are any such rows in the DS5ata frame
>      if(sum(calc_rows)) {
>        cat(IM,Tax,Name,"\n")
>        # if so, fill in the four values for these rows
>VC[calc_rows]  <- D2L[1] * (1-
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) ) +
>D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ",")))) +
>D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ",")))) +
>D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))) +
>D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))
>        print(unique(VC ))
>      }
>    }
>  }
>
>Vul <- distinct(d1[,c(65,4,3)])
>
>dim(VC) <- c(length(unlist(str_split(as.character(d1[1,]$Y_vals),
>pattern = ","))),length(distinct(d1[,c(65,4,3)])$Name))  ## (rows,
>cols)
>VC
>VC_t <- t(VC)
>Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse =
>',')))
>
>Vul$Y_vals <- Vulnerability
>
>
>
>
>Best,
>ioanna
>
>
>
>
>
>
>
>
>
>
>Name    Taxonomy        Damage_State    Y_vals
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1// Slight
>4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.999989939
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>Collapse      
>0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>Extensive    
>5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>Moderate      
>0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2    
>Collapse      
>0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2    
>Extensive    
>0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2    
>Moderate      
>0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2     Slight
>0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Collapse      
>0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Extensive    
>0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Moderate      
>0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Slight
>0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save output of multiple loops in a matrix

ioanna
Hello again,

Here is the reproducible example:

rm(list = ls())
library(plyr)
library(dplyr)
library( data.table)
library(stringr)


d1 <- data.frame( Name = rep(c('Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)',
                               'Rojas(2010) - CR/LFM/DNO 2storey',
                               'Rojas(2010) - CR/LFM/DNO 3storey'), each = 4),
                  Taxonomy = rep(c('CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//',
                                   'CR/LFM/DNO/H:2/EDU2',
                                   'CR/LFM/DNO/H:3'), each = 4),
                  Damage_State =rep(c('Collapse', 'Extensive', 'Moderate', 'Slight'), times =3),
                  Y_vals = c('0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106',
                             '5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531',
                             '0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227',
                             '4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.999989939',

                             '0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1',
                             '0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
                             '0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
                             '0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',

                             '0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1',
                             '0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
                             '0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
                             '0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1')
                             )




D2L <- c(0, 2, 10, 50, 100)

VC_final <- array(NA, length(distinct(d1[,c(1,2)])$Name) )

# get the rows for the four damage states
DS1_rows <- d1$Damage_State ==  unique(d1$Damage_State)[4]
DS2_rows <- d1$Damage_State ==  unique(d1$Damage_State)[3]
DS3_rows <- d1$Damage_State ==  unique(d1$Damage_State)[2]
DS4_rows <- d1$Damage_State ==  unique(d1$Damage_State)[1]

# step through all possible values of IM.type and Taxonomy and Name
#### This is true for this subset not generalibale needs to be checked first ##
VC       <- matrix(NA, 3,26)
  for(Tax in unique(d1$Taxonomy)) {
    for(Name in unique(d1$Name)) {
      # get a logical vector of the rows to be use DS5 in this calculation
      calc_rows <-  d1$Taxonomy == Tax & d1$Name == Name


      # check that there are any such rows in the DS5ata frame
      if(sum(calc_rows)) {

        cat(Tax,Name,"\n")
        # if so, fill in the four values for these rows
        VC[calc_rows]  <- D2L[1] * (1- as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) ) +
          D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) -
                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ",")))) +
          D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ","))) -
                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ",")))) +
          D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ","))) -
                      as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))) +
          D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))
        print(VC[calc_rows] )
      }
    }
  }



Vul <- distinct(d1[,c(1,2)])

dim(VC) <- c(length(unlist(str_split(as.character(d1[2,]$Y_vals), pattern = ","))),length(distinct(d1[,c(1,2)])$Name))  ## (rows, cols)
VC
VC_t <- t(VC)
Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse = ',')))

Vul$Y_vals <- Vulnerability




________________________________
From: Jeff Newmiller <[hidden email]>
Sent: 21 March 2020 16:27
To: [hidden email] <[hidden email]>; Ioanna Ioannou <[hidden email]>; [hidden email] <[hidden email]>
Subject: Re: [R] How to save output of multiple loops in a matrix

You have again posted using HTML  and the result is unreadable. Please post a reproducible example using dput instead of assuming we can read your formatted code or table.

On March 21, 2020 8:59:58 AM PDT, Ioanna Ioannou <[hidden email]> wrote:

>Hello everyone,
>
>I am having this data.frame. For each row you have 26 values aggregated
>in a cell and separated by a comma. I want to do some calculations for
>all unique names and taxonomy which include the four different damage
>states. I can estimate the results but i am struggling to save them in
>a data.frame and assign next to them the unique combination of the
>name, taxonomy. Any help much appreciated.
>
>
>d1 <- read.csv('test.csv')
>
>D2L <- c(0, 2, 10, 50, 100)
>
>VC_final <- array(NA, length(distinct(d1[,c(65,4,3)])$Name) )
>VC       <- matrix(NA,
>length(distinct(d1[,c(65,4,3)])$Name),length(unlist(str_split(as.character(d1[1,]$Y_vals),
>pattern = ","))))
>
># get the rows for the four damage states
>DS1_rows <- d1$Damage_State ==  unique(d1$Damage_State)[4]
>DS2_rows <- d1$Damage_State ==  unique(d1$Damage_State)[3]
>DS3_rows <- d1$Damage_State ==  unique(d1$Damage_State)[2]
>DS4_rows <- d1$Damage_State ==  unique(d1$Damage_State)[1]
>
># step through all possible values of IM.type and Taxonomy and Name
>#### This is true for this subset not generalibale needs to be checked
>first ##
>
>for(IM in unique(d1$IM_type)) {
>  for(Tax in unique(d1$Taxonomy)) {
>    for(Name in unique(d1$Name)) {
>   # get a logical vector of the rows to be use DS5 in this calculation
>   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name
>
>
>      # check that there are any such rows in the DS5ata frame
>      if(sum(calc_rows)) {
>        cat(IM,Tax,Name,"\n")
>        # if so, fill in the four values for these rows
>VC[calc_rows]  <- D2L[1] * (1-
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) ) +
>D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ",")))) +
>D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ",")))) +
>D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))) +
>D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))
>        print(VC[calc_rows] )
>      }
>    }
>  }
>}
>
>  for(Tax in unique(d1$Taxonomy)) {
>    for(Name in unique(d1$Name)) {
>   # get a logical vector of the rows to be use DS5 in this calculation
>   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name
>
>
>      # check that there are any such rows in the DS5ata frame
>      if(sum(calc_rows)) {
>        cat(IM,Tax,Name,"\n")
>        # if so, fill in the four values for these rows
>VC[calc_rows]  <- D2L[1] * (1-
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) ) +
>D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS1_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ",")))) +
>D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS2_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ",")))) +
>D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS3_rows,]$Y_vals), pattern = ","))) -
>as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))) +
>D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
>DS4_rows,]$Y_vals), pattern = ",")))
>        print(unique(VC ))
>      }
>    }
>  }
>
>Vul <- distinct(d1[,c(65,4,3)])
>
>dim(VC) <- c(length(unlist(str_split(as.character(d1[1,]$Y_vals),
>pattern = ","))),length(distinct(d1[,c(65,4,3)])$Name))  ## (rows,
>cols)
>VC
>VC_t <- t(VC)
>Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse =
>',')))
>
>Vul$Y_vals <- Vulnerability
>
>
>
>
>Best,
>ioanna
>
>
>
>
>
>
>
>
>
>
>Name    Taxonomy        Damage_State    Y_vals
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1// Slight
>4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.9
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>Collapse
>0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>Extensive
>5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531
>Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>Moderate
>0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
>Collapse
>0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
>Extensive
>0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
>Moderate
>0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2     Slight
>0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Collapse
>0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Extensive
>0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Moderate
>0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Slight
>0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>
>
>       [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save output of multiple loops in a matrix

Jeff Newmiller
You really need to pick small problems and build solutions for them as
functions rather than copy-pasting code. Then you can build more
complicated solutions using those small solutions that can actually be
understood. I suspect that in a few days you would not understand your own
code because it is so complicated... but that is not inevitable... you
_can_ avoid creating write-only code.

library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#>     filter, lag
#> The following objects are masked from 'package:base':
#>
#>     intersect, setdiff, setequal, union
library(tidyr)

damage_states <- c( 'Collapse', 'Extensive', 'Moderate', 'Slight')
# the following re-definition of d1 uses the stringsAsFactors parameter to
# prevent the character data from automatically being converted to
# factors, which is much better than having to use as.character later
# throughout your code.
d3 <- data.frame( Name = rep(c( 'Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)'
                               , 'Rojas(2010) - CR/LFM/DNO 2storey'
                               , 'Rojas(2010) - CR/LFM/DNO 3storey'
                               )
                             , each = 4
                             )
                 , Taxonomy = rep(c( 'CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//'
                                   , 'CR/LFM/DNO/H:2/EDU2'
                                   , 'CR/LFM/DNO/H:3'
                                   )
                                 , each = 4
                                 )
                 , Damage_State = rep( damage_states, times = 3 )
                 , Y_vals = c( '0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106'
                             , '5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531'
                             , '0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227'
                             , '4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.999989939'
                             , '0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             , '0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             , '0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             , '0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             , '0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1'
                             , '0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             , '0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             , '0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1'
                             )
                 , stringsAsFactors = FALSE
                 )
# convert Damage_State to a factor using the desired sequence of levels
# This is useful when spread'ing the values to create columns in the
# desired order
d3$Damage_State = factor( d3$Damage_State, levels = damage_states )
d3long <- (   d3
           %>% separate_rows( Y_vals, sep="," )
           %>% mutate( Y_vals = as.numeric( Y_vals ) )
           )
str( d3long )
#> 'data.frame':    312 obs. of  4 variables:
#>  $ Name        : chr  "Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)" "Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)" "Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)" "Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)" ...
#>  $ Taxonomy    : chr  "CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//" "CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//" "CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//" "CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//" ...
#>  $ Damage_State: Factor w/ 4 levels "Collapse","Extensive",..: 1 1 1 1 1 1 1 1 1 1 ...
#>  $ Y_vals      : num  0.00 6.49e-45 1.29e-29 3.35e-22 1.25e-17 ...

# By putting the vector of weights into a data frame you can make the
# meaning of the vector D1L self-documenting
weights <- data.frame( Damage_State = c( "Initial", rev( damage_states ) )
                      , D2L = c( 0, 2, 10, 50, 100 )
                      )
weights
#>   Damage_State D2L
#> 1      Initial   0
#> 2       Slight   2
#> 3     Moderate  10
#> 4    Extensive  50
#> 5     Collapse 100

# Group-by to get one row per combination of Taxonomy, Damage_State,
# and Name, then create a position index value for each Y_vals value
# then split the dataframe by combinations of Taxonomy and Name. Each
# small data frame in the `data` column has its own columns Damage_State,
# Index, and Y_vals.
d3long1 <- (   d3long
            %>% group_by( Taxonomy, Damage_State, Name )
            %>% mutate( Index = seq.int( n() ) )
            %>% ungroup()
            %>% nest( data = -c( Taxonomy, Name ) )
            )
d3long1
#> # A tibble: 3 x 3
#>   Name                           Taxonomy                            data
#>   <chr>                          <chr>                               <list>
#> 1 Hancilar et. al (2014) - CR/L~ CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990~ <tibble [1~
#> 2 Rojas(2010) - CR/LFM/DNO 2sto~ CR/LFM/DNO/H:2/EDU2                 <tibble [1~
#> 3 Rojas(2010) - CR/LFM/DNO 3sto~ CR/LFM/DNO/H:3                      <tibble [1~
# function to convert a sub-dataframe into a matrix,
# built with tidyverse tools
DFL_to_matrix <- function( DFL, labelCol, indexCol, valueCol ) {
     stopifnot( 3 == length( DFL ) )
     (   DFL
     %>% spread( !!labelCol, !!valueCol )
     %>% select( -!!indexCol )
     %>% as.matrix
     )
}
# same function built with base R
DFL_to_matrix2 <- function( DFL, labelCol, indexCol, valueCol ) {
     as.matrix( data.frame( split( DFL[[ valueCol ]]
                                 , DFL[[ labelCol ]]
                                 )
                          )
              )
}
# you can test small functions and later re-use them
#DFL_to_matrix2( d3long1$data[[1]], "Damage_State", "Index", "Y_vals" )

# Now convert the long data frames into matrices
# then use matrix multiplication to weight the damage
d3long2 <- (   d3long1
            %>% mutate( mat = lapply( data
                                    , DFL_to_matrix2
                                    , labelCol = "Damage_State"
                                    , indexCol = "Index"
                                    , valueCol = "Y_vals"
                                    )
                      , vc = lapply( mat
                                   , function( m ) {
                                         mr <- m[ , rev( seq.int( ncol( m ) ) ) ]
                                         m1 <- cbind( matrix( 1, nrow = nrow( m ) )
                                                    , mr
                                                    )
                                         m0 <- cbind( mr
                                                    , matrix( 0, nrow = nrow( m ) )
                                                    )
                                         c( ( m1 - m0 ) %*% weights$D2L )
                                     }
                                   )
                      )
            )
str( d3long2[ 1,] )
#> Classes 'tbl_df', 'tbl' and 'data.frame':    1 obs. of  5 variables:
#>  $ Name    : chr "Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)"
#>  $ Taxonomy: chr "CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//"
#>  $ data    :List of 1
#>   ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 104 obs. of  3 variables:
#>   .. ..$ Damage_State: Factor w/ 4 levels "Collapse","Extensive",..: 1 1 1 1 1 1 1 1 1 1 ...
#>   .. ..$ Y_vals      : num  0.00 6.49e-45 1.29e-29 3.35e-22 1.25e-17 ...
#>   .. ..$ Index       : int  1 2 3 4 5 6 7 8 9 10 ...
#>  $ mat     :List of 1
#>   ..$ : num [1:26, 1:4] 0.00 6.49e-45 1.29e-29 3.35e-22 1.25e-17 ...
#>   .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..$ : NULL
#>   .. .. ..$ : chr  "Collapse" "Extensive" "Moderate" "Slight"
#>  $ vc      :List of 1
#>   ..$ : num  9.22e-149 1.45e-02 3.17e-01 8.82e-01 1.41 ...
d3long3 <- (   d3long2
            %>% select( Name, Taxonomy, vc )
            %>% mutate( Y_vals = unlist( lapply( vc
                                               , function(v){paste(v,collapse = ",") } )))
            %>% select( -vc )
            )
d3long3
#> # A tibble: 3 x 3
#>   Name                  Taxonomy                   Y_vals
#>   <chr>                 <chr>                      <chr>
#> 1 Hancilar et. al (201~ CR/LDUAL/HEX:4+HFEX:12.8/~ 9.22e-149,0.01446893292,0.31~
#> 2 Rojas(2010) - CR/LFM~ CR/LFM/DNO/H:2/EDU2        0,2.610109488,10.000712,35.8~
#> 3 Rojas(2010) - CR/LFM~ CR/LFM/DNO/H:3             0,0.737817568,9.939968168001~


On Sat, 21 Mar 2020, Ioanna Ioannou wrote:

> Hello again,
>
> Here is the reproducible example:
>
> rm(list = ls())
> library(plyr)
> library(dplyr)
> library( data.table)
> library(stringr)
>
>
> d1 <- data.frame( Name = rep(c('Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)',
>                               'Rojas(2010) - CR/LFM/DNO 2storey',
>                               'Rojas(2010) - CR/LFM/DNO 3storey'), each = 4),
>                  Taxonomy = rep(c('CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//',
>                                   'CR/LFM/DNO/H:2/EDU2',
>                                   'CR/LFM/DNO/H:3'), each = 4),
>                  Damage_State =rep(c('Collapse', 'Extensive', 'Moderate', 'Slight'), times =3),
>                  Y_vals = c('0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106',
>                             '5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531',
>                             '0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227',
>                             '4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.999989939',
>
>                             '0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1',
>                             '0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>                             '0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>                             '0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>
>                             '0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1',
>                             '0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>                             '0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>                             '0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1')
>                             )
>
>
>
>
> D2L <- c(0, 2, 10, 50, 100)
>
> VC_final <- array(NA, length(distinct(d1[,c(1,2)])$Name) )
>
> # get the rows for the four damage states
> DS1_rows <- d1$Damage_State ==  unique(d1$Damage_State)[4]
> DS2_rows <- d1$Damage_State ==  unique(d1$Damage_State)[3]
> DS3_rows <- d1$Damage_State ==  unique(d1$Damage_State)[2]
> DS4_rows <- d1$Damage_State ==  unique(d1$Damage_State)[1]
>
> # step through all possible values of IM.type and Taxonomy and Name
> #### This is true for this subset not generalibale needs to be checked first ##
> VC       <- matrix(NA, 3,26)
>  for(Tax in unique(d1$Taxonomy)) {
>    for(Name in unique(d1$Name)) {
>      # get a logical vector of the rows to be use DS5 in this calculation
>      calc_rows <-  d1$Taxonomy == Tax & d1$Name == Name
>
>
>      # check that there are any such rows in the DS5ata frame
>      if(sum(calc_rows)) {
>
>        cat(Tax,Name,"\n")
>        # if so, fill in the four values for these rows
>        VC[calc_rows]  <- D2L[1] * (1- as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) ) +
>          D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS1_rows,]$Y_vals), pattern = ","))) -
>                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ",")))) +
>          D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS2_rows,]$Y_vals), pattern = ","))) -
>                     as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ",")))) +
>          D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows & DS3_rows,]$Y_vals), pattern = ","))) -
>                      as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))) +
>          D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows & DS4_rows,]$Y_vals), pattern = ",")))
>        print(VC[calc_rows] )
>      }
>    }
>  }
>
>
>
> Vul <- distinct(d1[,c(1,2)])
>
> dim(VC) <- c(length(unlist(str_split(as.character(d1[2,]$Y_vals), pattern = ","))),length(distinct(d1[,c(1,2)])$Name))  ## (rows, cols)
> VC
> VC_t <- t(VC)
> Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse = ',')))
>
> Vul$Y_vals <- Vulnerability
>
>
>
>
> ________________________________
> From: Jeff Newmiller <[hidden email]>
> Sent: 21 March 2020 16:27
> To: [hidden email] <[hidden email]>; Ioanna Ioannou <[hidden email]>; [hidden email] <[hidden email]>
> Subject: Re: [R] How to save output of multiple loops in a matrix
>
> You have again posted using HTML  and the result is unreadable. Please post a reproducible example using dput instead of assuming we can read your formatted code or table.
>
> On March 21, 2020 8:59:58 AM PDT, Ioanna Ioannou <[hidden email]> wrote:
>> Hello everyone,
>>
>> I am having this data.frame. For each row you have 26 values aggregated
>> in a cell and separated by a comma. I want to do some calculations for
>> all unique names and taxonomy which include the four different damage
>> states. I can estimate the results but i am struggling to save them in
>> a data.frame and assign next to them the unique combination of the
>> name, taxonomy. Any help much appreciated.
>>
>>
>> d1 <- read.csv('test.csv')
>>
>> D2L <- c(0, 2, 10, 50, 100)
>>
>> VC_final <- array(NA, length(distinct(d1[,c(65,4,3)])$Name) )
>> VC       <- matrix(NA,
>> length(distinct(d1[,c(65,4,3)])$Name),length(unlist(str_split(as.character(d1[1,]$Y_vals),
>> pattern = ","))))
>>
>> # get the rows for the four damage states
>> DS1_rows <- d1$Damage_State ==  unique(d1$Damage_State)[4]
>> DS2_rows <- d1$Damage_State ==  unique(d1$Damage_State)[3]
>> DS3_rows <- d1$Damage_State ==  unique(d1$Damage_State)[2]
>> DS4_rows <- d1$Damage_State ==  unique(d1$Damage_State)[1]
>>
>> # step through all possible values of IM.type and Taxonomy and Name
>> #### This is true for this subset not generalibale needs to be checked
>> first ##
>>
>> for(IM in unique(d1$IM_type)) {
>>  for(Tax in unique(d1$Taxonomy)) {
>>    for(Name in unique(d1$Name)) {
>>   # get a logical vector of the rows to be use DS5 in this calculation
>>   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name
>>
>>
>>      # check that there are any such rows in the DS5ata frame
>>      if(sum(calc_rows)) {
>>        cat(IM,Tax,Name,"\n")
>>        # if so, fill in the four values for these rows
>> VC[calc_rows]  <- D2L[1] * (1-
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS1_rows,]$Y_vals), pattern = ","))) ) +
>> D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS1_rows,]$Y_vals), pattern = ","))) -
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS2_rows,]$Y_vals), pattern = ",")))) +
>> D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS2_rows,]$Y_vals), pattern = ","))) -
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS3_rows,]$Y_vals), pattern = ",")))) +
>> D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS3_rows,]$Y_vals), pattern = ","))) -
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS4_rows,]$Y_vals), pattern = ",")))) +
>> D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS4_rows,]$Y_vals), pattern = ",")))
>>        print(VC[calc_rows] )
>>      }
>>    }
>>  }
>> }
>>
>>  for(Tax in unique(d1$Taxonomy)) {
>>    for(Name in unique(d1$Name)) {
>>   # get a logical vector of the rows to be use DS5 in this calculation
>>   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name == Name
>>
>>
>>      # check that there are any such rows in the DS5ata frame
>>      if(sum(calc_rows)) {
>>        cat(IM,Tax,Name,"\n")
>>        # if so, fill in the four values for these rows
>> VC[calc_rows]  <- D2L[1] * (1-
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS1_rows,]$Y_vals), pattern = ","))) ) +
>> D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS1_rows,]$Y_vals), pattern = ","))) -
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS2_rows,]$Y_vals), pattern = ",")))) +
>> D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS2_rows,]$Y_vals), pattern = ","))) -
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS3_rows,]$Y_vals), pattern = ",")))) +
>> D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS3_rows,]$Y_vals), pattern = ","))) -
>> as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS4_rows,]$Y_vals), pattern = ",")))) +
>> D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
>> DS4_rows,]$Y_vals), pattern = ",")))
>>        print(unique(VC ))
>>      }
>>    }
>>  }
>>
>> Vul <- distinct(d1[,c(65,4,3)])
>>
>> dim(VC) <- c(length(unlist(str_split(as.character(d1[1,]$Y_vals),
>> pattern = ","))),length(distinct(d1[,c(65,4,3)])$Name))  ## (rows,
>> cols)
>> VC
>> VC_t <- t(VC)
>> Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse =
>> ',')))
>>
>> Vul$Y_vals <- Vulnerability
>>
>>
>>
>>
>> Best,
>> ioanna
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Name    Taxonomy        Damage_State    Y_vals
>> Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>> CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1// Slight
>> 4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.999985325,0.9
>> Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>> CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>> Collapse
>> 0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.12787106
>> Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>> CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>> Extensive
>> 5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798268,0.607498531
>> Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
>> CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
>> Moderate
>> 0,1.05e-10,4.75e-06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.281721077,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.97543154,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227
>> Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
>> Collapse
>> 0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
>> Extensive
>> 0,1.21e-32,1.78e-05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
>> Moderate
>> 0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2     Slight
>> 0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Collapse
>> 0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.998997836,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Extensive
>> 0,2.12e-51,4.89e-14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Moderate
>> 0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>> Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Slight
>> 0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to save output of multiple loops in a matrix

PIKAL Petr
In reply to this post by ioanna
Hi

What about instead of using split inside several cycles prepare new data
frame and perform required calculation on it
E.g. for the first row you get from second ant fourth column following
(26,2) matrix.

cbind(d1[1,3],unlist(strsplit(d1[1,4],",")))
      [,1]       [,2]        
 [1,] "Collapse" "0"          
 [2,] "Collapse" "6.49e-45"  
 [3,] "Collapse" "1.29e-29"  
 [4,] "Collapse" "3.35e-22"  
 [5,]
....

you could easily cycle through rows by

lll <- vector("list", 12)
for (i in 1:12) {

lll[[i]] <- cbind(d1[i,3],unlist(strsplit(d1[i,4],",")))
}

str(lll)
List of 12
 $ : chr [1:26, 1:2] "Collapse" "Collapse" "Collapse" "Collapse" ...
 $ : chr [1:26, 1:2] "Extensive" "Extensive" "Extensive" "Extensive" ...
 $ : chr [1:26, 1:2] "Moderate" "Moderate" "Moderate" "Moderate" ...

by do call you could change it to matrix and/or data.frame
> ddd <- data.frame(do.call(rbind, lll))
> head(ddd)
        X1       X2
1 Collapse        0
2 Collapse 6.49e-45
3 Collapse 1.29e-29
4 Collapse 3.35e-22
5 Collapse 1.25e-17
6 Collapse  1.8e-14
> str(data.frame(ddd))
'data.frame':   312 obs. of  2 variables:
 $ X1: chr  "Collapse" "Collapse" "Collapse" "Collapse" ...
 $ X2: chr  "0" "6.49e-45" "1.29e-29" "3.35e-22" ...
> dim(ddd)
[1] 312   2
>

I am not sure if this is what you want but my feeling is that it makes
further calculation easier.

S pozdravem | Best Regards
RNDr. Petr PIKAL
Vedoucí Výzkumu a vývoje | Research Manager
PRECHEZA a.s.
nábř. Dr. Edvarda Beneše 1170/24 | 750 02 Přerov | Czech Republic
Tel: +420 581 252 256 | GSM: +420 724 008 364
[hidden email] | www.precheza.cz

Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních
partnerů PRECHEZA a.s. jsou zveřejněny na:
https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about
processing and protection of business partner's personal data are available
on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné
a podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti:
https://www.precheza.cz/01-dovetek/ | This email and any documents attached
to it may be confidential and are subject to the legally binding disclaimer:
https://www.precheza.cz/en/01-disclaimer/

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Ioanna Ioannou
> Sent: Saturday, March 21, 2020 5:54 PM
> To: [hidden email]
> Subject: Re: [R] How to save output of multiple loops in a matrix
>
> Hello again,
>
> Here is the reproducible example:
>
> rm(list = ls())
> library(plyr)
> library(dplyr)
> library( data.table)
> library(stringr)
>
>
> d1 <- data.frame( Name = rep(c('Hancilar et. al (2014) - CR/LDUAL school -
> Case V (Sd)',
>                                'Rojas(2010) - CR/LFM/DNO 2storey',
>                                'Rojas(2010) - CR/LFM/DNO 3storey'), each =
4),
>                   Taxonomy =
> rep(c('CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1/
> /',
>                                    'CR/LFM/DNO/H:2/EDU2',
>                                    'CR/LFM/DNO/H:3'), each = 4),
>                   Damage_State =rep(c('Collapse', 'Extensive', 'Moderate',
'Slight'),
> times =3),
>                   Y_vals =
c('0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-

> 12,2.35e-10,6.18e-09,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-
> 05,0.000274154,0.000736426,0.001740046,0.003688955,0.007130224,0.012730
> 071,0.021221055,0.0333283,0.049687895,0.070771949,0.096832412,0.1278710
> 6',
>                              '5.02e-182,3.52e-10,8.81e-07,3.62e-
> 05,0.000346166,0.001608096,0.004916965,0.01150426,0.022416772,0.0383110
> 15,0.059392175,0.085458446,0.115998702,0.150303282,0.187564259,0.226954
> 808,0.267685669,0.309041053,0.35039806,0.391233913,0.431124831,0.469739
> 614,0.506830242,0.542221151,0.575798268,0.607498531',
>                              '0,1.05e-10,4.75e-
> 06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.2817210
> 77,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636
> 781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.9754315
> 4,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227',
>                              '4.61e-
> 149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0.90131
> 2438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876,0.9968
> 69188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,0.99982
> 7443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407,0.9999
> 85325,0.999989939',
>
>
'0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-
> 05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998
> 652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1',
>                              '0,1.21e-32,1.78e-
>
05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
,1'
> ,
>
'0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>
'0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>
>
'0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-
> 08,6.42e-
> 05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.9989978
> 36,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1',
>                              '0,2.12e-51,4.89e-
>
14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1
> ,1,1,1,1,1,1',
>                              '0,3.22e-
> 07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1',
>
'0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1')

>                              )
>
>
>
>
> D2L <- c(0, 2, 10, 50, 100)
>
> VC_final <- array(NA, length(distinct(d1[,c(1,2)])$Name) )
>
> # get the rows for the four damage states DS1_rows <- d1$Damage_State ==
> unique(d1$Damage_State)[4] DS2_rows <- d1$Damage_State ==
> unique(d1$Damage_State)[3] DS3_rows <- d1$Damage_State ==
> unique(d1$Damage_State)[2] DS4_rows <- d1$Damage_State ==
> unique(d1$Damage_State)[1]
>
> # step through all possible values of IM.type and Taxonomy and Name ####
> This is true for this subset not generalibale needs to be checked first ##
> VC       <- matrix(NA, 3,26)
>   for(Tax in unique(d1$Taxonomy)) {
>     for(Name in unique(d1$Name)) {
>       # get a logical vector of the rows to be use DS5 in this calculation
>       calc_rows <-  d1$Taxonomy == Tax & d1$Name == Name
>
>
>       # check that there are any such rows in the DS5ata frame
>       if(sum(calc_rows)) {
>
>         cat(Tax,Name,"\n")
>         # if so, fill in the four values for these rows
>         VC[calc_rows]  <- D2L[1] * (1-
> as.numeric(unlist(str_split(as.character(d1[calc_rows &
DS1_rows,]$Y_vals),
> pattern = ","))) ) +
>           D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
> DS1_rows,]$Y_vals), pattern = ","))) -
>                      as.numeric(unlist(str_split(as.character(d1[calc_rows
&
> DS2_rows,]$Y_vals), pattern = ",")))) +
>           D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
> DS2_rows,]$Y_vals), pattern = ","))) -
>                      as.numeric(unlist(str_split(as.character(d1[calc_rows
&
> DS3_rows,]$Y_vals), pattern = ",")))) +
>           D2L[4] * (as.numeric(unlist(str_split(as.character(d1[calc_rows
&
> DS3_rows,]$Y_vals), pattern = ","))) -
>
as.numeric(unlist(str_split(as.character(d1[calc_rows &
> DS4_rows,]$Y_vals), pattern = ",")))) +
>           D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows
&

> DS4_rows,]$Y_vals), pattern = ",")))
>         print(VC[calc_rows] )
>       }
>     }
>   }
>
>
>
> Vul <- distinct(d1[,c(1,2)])
>
> dim(VC) <- c(length(unlist(str_split(as.character(d1[2,]$Y_vals), pattern
=
> ","))),length(distinct(d1[,c(1,2)])$Name))  ## (rows, cols) VC VC_t <-
t(VC)
> Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse =
',')))

>
> Vul$Y_vals <- Vulnerability
>
>
>
>
> ________________________________
> From: Jeff Newmiller <[hidden email]>
> Sent: 21 March 2020 16:27
> To: [hidden email] <[hidden email]>; Ioanna Ioannou
> <[hidden email]>; [hidden email] <[hidden email]>
> Subject: Re: [R] How to save output of multiple loops in a matrix
>
> You have again posted using HTML  and the result is unreadable. Please
post

> a reproducible example using dput instead of assuming we can read your
> formatted code or table.
>
> On March 21, 2020 8:59:58 AM PDT, Ioanna Ioannou <[hidden email]>
> wrote:
> >Hello everyone,
> >
> >I am having this data.frame. For each row you have 26 values aggregated
> >in a cell and separated by a comma. I want to do some calculations for
> >all unique names and taxonomy which include the four different damage
> >states. I can estimate the results but i am struggling to save them in
> >a data.frame and assign next to them the unique combination of the
> >name, taxonomy. Any help much appreciated.
> >
> >
> >d1 <- read.csv('test.csv')
> >
> >D2L <- c(0, 2, 10, 50, 100)
> >
> >VC_final <- array(NA, length(distinct(d1[,c(65,4,3)])$Name) )
> >VC       <- matrix(NA,
> >length(distinct(d1[,c(65,4,3)])$Name),length(unlist(str_split(as.charac
> >ter(d1[1,]$Y_vals),
> >pattern = ","))))
> >
> ># get the rows for the four damage states DS1_rows <- d1$Damage_State
> >==  unique(d1$Damage_State)[4] DS2_rows <- d1$Damage_State ==
> >unique(d1$Damage_State)[3] DS3_rows <- d1$Damage_State ==
> >unique(d1$Damage_State)[2] DS4_rows <- d1$Damage_State ==
> >unique(d1$Damage_State)[1]
> >
> ># step through all possible values of IM.type and Taxonomy and Name
> >#### This is true for this subset not generalibale needs to be checked
> >first ##
> >
> >for(IM in unique(d1$IM_type)) {
> >  for(Tax in unique(d1$Taxonomy)) {
> >    for(Name in unique(d1$Name)) {
> >   # get a logical vector of the rows to be use DS5 in this calculation
> >   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name ==
> Name
> >
> >
> >      # check that there are any such rows in the DS5ata frame
> >      if(sum(calc_rows)) {
> >        cat(IM,Tax,Name,"\n")
> >        # if so, fill in the four values for these rows VC[calc_rows]
> ><- D2L[1] * (1- as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS1_rows,]$Y_vals), pattern = ","))) ) +
> >D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS1_rows,]$Y_vals), pattern = ","))) -
> >as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS2_rows,]$Y_vals), pattern = ",")))) +
> >D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS2_rows,]$Y_vals), pattern = ","))) -
> >as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS3_rows,]$Y_vals), pattern = ",")))) + D2L[4] *
> >(as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS3_rows,]$Y_vals), pattern = ","))) -
> >as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS4_rows,]$Y_vals), pattern = ",")))) +
> >D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS4_rows,]$Y_vals), pattern = ",")))
> >        print(VC[calc_rows] )
> >      }
> >    }
> >  }
> >}
> >
> >  for(Tax in unique(d1$Taxonomy)) {
> >    for(Name in unique(d1$Name)) {
> >   # get a logical vector of the rows to be use DS5 in this calculation
> >   calc_rows <- d1$IM_type == IM & d1$Taxonomy == Tax & d1$Name ==
> Name
> >
> >
> >      # check that there are any such rows in the DS5ata frame
> >      if(sum(calc_rows)) {
> >        cat(IM,Tax,Name,"\n")
> >        # if so, fill in the four values for these rows VC[calc_rows]
> ><- D2L[1] * (1- as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS1_rows,]$Y_vals), pattern = ","))) ) +
> >D2L[2]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS1_rows,]$Y_vals), pattern = ","))) -
> >as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS2_rows,]$Y_vals), pattern = ",")))) +
> >D2L[3]* (as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS2_rows,]$Y_vals), pattern = ","))) -
> >as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS3_rows,]$Y_vals), pattern = ",")))) + D2L[4] *
> >(as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS3_rows,]$Y_vals), pattern = ","))) -
> >as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS4_rows,]$Y_vals), pattern = ",")))) +
> >D2L[5]*    as.numeric(unlist(str_split(as.character(d1[calc_rows &
> >DS4_rows,]$Y_vals), pattern = ",")))
> >        print(unique(VC ))
> >      }
> >    }
> >  }
> >
> >Vul <- distinct(d1[,c(65,4,3)])
> >
> >dim(VC) <- c(length(unlist(str_split(as.character(d1[1,]$Y_vals),
> >pattern = ","))),length(distinct(d1[,c(65,4,3)])$Name))  ## (rows,
> >cols)
> >VC
> >VC_t <- t(VC)
> >Vulnerability <- matrix(apply(VC_t, 1, function(x) paste(x, collapse =
> >',')))
> >
> >Vul$Y_vals <- Vulnerability
> >
> >
> >
> >
> >Best,
> >ioanna
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >Name    Taxonomy        Damage_State    Y_vals
> >Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
> >CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
> Slight
> >4.61e-149,0.007234459,0.158482316,0.438164341,0.671470035,0.818341464,0
> >.901312438,0.946339742,0.970531767,0.983584997,0.990707537,0.994650876
> ,
> >0.996869188,0.998137671,0.998874868,0.9993101,0.999570978,0.999729626,
> 0
> >.999827443,0.999888548,0.999927197,0.999951931,0.999967938,0.999978407
> ,
> >0.999985325,0.9 Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
> >CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
> >Collapse
> >0,6.49e-45,1.29e-29,3.35e-22,1.25e-17,1.8e-14,3.81e-12,2.35e-10,6.18e-0
> >9,8.78e-08,7.86e-07,4.92e-06,2.32e-05,8.76e-05,0.000274154,0.000736426,
> >0.001740046,0.003688955,0.007130224,0.012730071,0.021221055,0.0333283,
> 0
> >.049687895,0.070771949,0.096832412,0.12787106
> >Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
> >CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
> >Extensive
> >5.02e-182,3.52e-10,8.81e-07,3.62e-05,0.000346166,0.001608096,0.00491696
> >5,0.01150426,0.022416772,0.038311015,0.059392175,0.085458446,0.1159987
> 0
> >2,0.150303282,0.187564259,0.226954808,0.267685669,0.309041053,0.350398
> 0
> >6,0.391233913,0.431124831,0.469739614,0.506830242,0.542221151,0.575798
> 2
> >68,0.607498531 Hancilar et. al (2014) - CR/LDUAL school - Case V (Sd)
> >CR/LDUAL/HEX:4+HFEX:12.8/YAPP:1990/EDU+EDU2//PLFSQ/IRRE//RSH1//
> >Moderate
> >0,1.05e-10,4.75e-
> 06,0.000479751,0.006156253,0.02983369,0.084284357,0.171401809,0.2817210
> 77,0.401071017,0.516782184,0.620508952,0.708327468,0.779597953,0.835636
> 781,0.87866127,0.911104254,0.935237852,0.95300803,0.965993954,0.9754315
> 4,0.982263787,0.987197155,0.990753887,0.993316294,0.99516227
> >Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
> >Collapse
> >0,4.91e-109,2.88e-47,3.32e-23,1.65e-11,1.78e-
> 05,0.018162775,0.356628282,0.870224163,0.992779045,0.999855873,0.999998
> 652,0.999999993,1,1,1,1,1,1,1,1,1,1,1,1,1
> >Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
> >Extensive
> >0,1.21e-32,1.78e-
>
05,0.645821244,0.999823159,0.999999999,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
,1

> >Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2
> >Moderate
> >0,0.077161367,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
> >Rojas(2010) - CR/LFM/DNO 2storey        CR/LFM/DNO/H:2/EDU2     Slight
> >0,0.996409276,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
> >Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Collapse
> >0,1.29e-144,1.99e-71,1.16e-40,3.23e-24,1.59e-14,1.41e-08,6.42e-
> 05,0.00971775,0.153727719,0.562404795,0.889217735,0.985915683,0.9989978
> 36,0.999955341,0.999998628,0.999999969,0.999999999,1,1,1,1,1,1,1,1
> >Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3   Extensive
> >0,2.12e-51,4.89e-
>
14,0.001339285,0.559153268,0.995244295,0.999997786,1,1,1,1,1,1,1,1,1,1,1,1,1

> ,1,1,1,1,1,1
> >Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Moderate
> >0,3.22e-07,0.992496021,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
> >Rojas(2010) - CR/LFM/DNO 3storey        CR/LFM/DNO/H:3  Slight
> >0,0.368907496,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
> >
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >[hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide
> >http://www.R-project.org/posting-guide.html
> >and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.