Reading dataset stored in vector form in R

Jyde

New Member
#1
I am new to R how can I read a dataset stored as vectors and with comments in it into R?

See Attached. Thanks in advance.
 

Dason

Ambassador to the humans
#2
Code:
setwd("C:/location/where/file/is/stored")
source("data.txt")
or just

Code:
source("C:/location/where/file/is/stored/data.txt")
 

Jyde

New Member
#3
Code:
setwd("C:/location/where/file/is/stored")
source("data.txt")
or just

Code:
source("C:/location/where/file/is/stored/data.txt")
That does it work.
The data is stored as vectors and there are comments in it. If you can download the attached dataset and see the actual structure that will help (it is not the usual data structure)
 

Dason

Ambassador to the humans
#4
I did. Can you explain why that doesn't work? Works for me...

The file is just R code for creating your datasets. So once you read it in (using source) the variables will be in R. So to see the data you'll need to type the data's name.

Code:
> source("data.txt")
> Country_Code
  [1] "ABW" "AFG" "AGO" "ALB" "ARE" "ARG" "ARM" "ATG" "AUS" "AUT" "AZE" "BDI"
 [13] "BEL" "BEN" "BFA" "BGD" "BGR" "BHR" "BHS" "BIH" "BLR" "BLZ" "BOL" "BRA"
 [25] "BRB" "BRN" "BTN" "BWA" "CAF" "CAN" "CHE" "CHL" "CHN" "CIV" "CMR" "COG"
 [37] "COL" "COM" "CPV" "CRI" "CUB" "CYP" "CZE" "DEU" "DJI" "DNK" "DOM" "DZA"
 [49] "ECU" "EGY" "ERI" "ESP" "EST" "ETH" "FIN" "FJI" "FRA" "FSM" "GAB" "GBR"
 [61] "GEO" "GHA" "GIN" "GMB" "GNB" "GNQ" "GRC" "GRD" "GTM" "GUM" "GUY" "HKG"
 [73] "HND" "HRV" "HTI" "HUN" "IDN" "IND" "IRL" "IRN" "IRQ" "ISL" "ITA" "JAM"
 [85] "JOR" "JPN" "KAZ" "KEN" "KGZ" "KHM" "KIR" "KOR" "KWT" "LAO" "LBN" "LBR"
 [97] "LBY" "LCA" "LKA" "LSO" "LTU" "LUX" "LVA" "MAC" "MAR" "MDA" "MDG" "MDV"
[109] "MEX" "MKD" "MLI" "MLT" "MMR" "MNE" "MNG" "MOZ" "MRT" "MUS" "MWI" "MYS"
[121] "NAM" "NCL" "NER" "NGA" "NIC" "NLD" "NOR" "NPL" "NZL" "OMN" "PAK" "PAN"
[133] "PER" "PHL" "PNG" "POL" "PRI" "PRT" "PRY" "PYF" "QAT" "ROU" "RUS" "RWA"
[145] "SAU" "SDN" "SEN" "SGP" "SLB" "SLE" "SLV" "SOM" "SSD" "STP" "SUR" "SVK"
[157] "SVN" "SWE" "SWZ" "SYR" "TCD" "TGO" "THA" "TJK" "TKM" "TLS" "TON" "TTO"
[169] "TUN" "TUR" "TZA" "UGA" "UKR" "URY" "USA" "UZB" "VCT" "VEN" "VIR" "VNM"
[181] "VUT" "WSM" "YEM" "ZAF" "COD" "ZMB" "ZWE"
> Life_Expectancy_At_Birth_1960
  [1] 65.56937 32.32851 32.98483 62.25437 52.24322 65.21554 65.86346 61.78273
  [9] 70.81707 68.58561 60.83624 41.23605 69.70195 37.27827 34.47790 45.82932
 [17] 69.24756 52.08937 62.72905 60.27622 67.70810 59.96137 42.11832 54.20546
 [25] 60.73805 62.50037 32.35937 50.54773 36.48263 71.13317 71.31341 57.45829
 [33] 43.46580 36.87241 41.52376 48.58163 56.71676 41.44244 48.85641 60.57620
 [41] 63.90466 69.59393 70.34878 69.31295 44.02127 72.17659 51.84527 46.13512
 [49] 53.21500 48.01371 37.36290 69.10927 67.90598 38.40571 68.81976 55.95849
 [57] 69.86829 57.58659 39.57012 71.12683 63.43185 45.83146 34.88639 32.04222
 [65] 37.84044 36.73305 68.16390 59.81593 45.53163 61.22634 60.27873 66.99971
 [73] 46.28832 64.60866 42.10010 68.00317 48.64032 41.17195 69.69176 44.94551
 [81] 48.03068 73.42866 69.12390 64.19183 52.68527 67.66610 58.36759 46.36241
 [89] 56.12807 41.23202 49.21598 53.00132 60.34795 43.20446 63.28012 34.78317
 [97] 42.64120 57.30376 59.74715 46.51071 69.84732 68.44639 69.78683 64.66093
[105] 48.44663 61.81278 39.97468 37.26863 57.06563 60.62280 28.21161 67.60178
[113] 42.73639 63.70561 48.36880 35.00371 43.48310 58.74522 37.77363 59.47534
[121] 46.88039 58.63902 35.51505 37.18295 46.99883 73.39268 73.54976 35.17083
[129] 71.23659 42.66707 45.29046 60.88171 47.69159 57.81193 38.46224 67.68049
[137] 68.71961 62.80893 63.79371 56.35705 61.20607 65.64244 66.05529 42.24929
[145] 45.66627 48.18763 38.20600 65.65983 49.38171 30.33154 49.94793 36.96588
[153] 31.67671 50.45137 59.68012 69.97593 68.97805 73.00561 44.23378 52.76824
[161] 38.01612 40.27283 54.69932 56.15354 54.45868 33.72712 61.36454 62.65759
[169] 42.00976 45.38441 43.65388 43.98356 68.29954 67.89639 69.77073 58.88554
[177] 57.72388 59.28512 63.73022 59.06702 46.48749 49.96951 34.36380 49.03629
[185] 41.01805 45.10980 51.54246
> Life_Expectancy_At_Birth_2013
  [1] 75.32866 60.02827 51.86617 77.53724 77.19563 75.98610 74.56137 75.77866
  [9] 82.19756 80.89024 70.69315 56.25161 80.38537 59.31202 58.24063 71.24524
 [17] 74.46585 76.54595 75.07354 76.27693 72.47073 69.98205 67.91344 74.12244
 [25] 75.33395 78.54666 69.10293 64.36080 49.87988 81.40112 82.74878 81.19793
 [33] 75.35302 51.20846 55.04180 61.66639 73.80973 62.93217 72.97237 79.22522
 [41] 79.25639 79.94978 78.27805 81.04390 61.68646 80.30244 73.31990 74.56895
 [49] 75.64851 70.92578 63.17788 82.42683 76.42439 63.44220 80.83171 69.91793
 [57] 81.96829 68.97339 63.84359 80.95610 74.07951 61.14207 58.21649 59.99927
 [65] 54.83841 57.29083 80.63415 73.19356 71.48639 78.87251 66.31002 83.83171
 [73] 72.94285 77.12683 62.40115 75.26829 68.70461 67.66041 81.04390 75.12598
 [81] 69.47168 83.11707 82.29024 73.46893 73.90141 83.33195 70.45000 60.95378
 [89] 70.20244 67.77205 65.76659 81.45976 74.46276 65.68724 80.12888 60.52039
 [97] 71.65768 74.91271 74.24029 49.33146 74.16341 81.79756 73.98049 80.33915
[105] 73.70905 68.81151 64.67390 76.60261 76.53266 75.18705 57.53520 80.74634
[113] 65.65410 74.75837 69.06180 54.64151 62.80271 74.46000 61.46600 74.56751
[121] 64.34388 77.12195 60.82815 52.44215 74.51476 81.10488 81.45122 69.22200
[129] 81.40732 76.84105 65.96368 77.41922 74.28385 68.13156 62.44917 76.84878
[137] 78.71120 80.37317 72.79917 76.33407 78.41849 74.46341 71.07317 63.39483
[145] 74.17763 63.16705 65.87876 82.34634 67.71893 50.36312 72.49815 55.02302
[153] 55.22090 66.25951 70.99000 76.26098 80.27805 81.70488 48.93793 74.71578
[161] 51.19149 59.13237 74.24693 69.40017 65.45656 67.52237 72.64034 70.30529
[169] 73.64634 75.17595 64.29183 57.76768 71.15951 76.83620 78.84146 68.22759
[177] 72.81088 74.07441 79.62439 75.75649 71.66924 73.25039 63.58351 56.73659
[185] 58.27193 59.23737 55.63300
 

Jyde

New Member
#5
I did. Can you explain why that doesn't work? Works for me...

The file is just R code for creating your datasets. So once you read it in (using source) the variables will be in R. So to see the data you'll need to type the data's name.

Code:
> source("data.txt")
> Country_Code
  [1] "ABW" "AFG" "AGO" "ALB" "ARE" "ARG" "ARM" "ATG" "AUS" "AUT" "AZE" "BDI"
 [13] "BEL" "BEN" "BFA" "BGD" "BGR" "BHR" "BHS" "BIH" "BLR" "BLZ" "BOL" "BRA"
 [25] "BRB" "BRN" "BTN" "BWA" "CAF" "CAN" "CHE" "CHL" "CHN" "CIV" "CMR" "COG"
 [37] "COL" "COM" "CPV" "CRI" "CUB" "CYP" "CZE" "DEU" "DJI" "DNK" "DOM" "DZA"
 [49] "ECU" "EGY" "ERI" "ESP" "EST" "ETH" "FIN" "FJI" "FRA" "FSM" "GAB" "GBR"
 [61] "GEO" "GHA" "GIN" "GMB" "GNB" "GNQ" "GRC" "GRD" "GTM" "GUM" "GUY" "HKG"
 [73] "HND" "HRV" "HTI" "HUN" "IDN" "IND" "IRL" "IRN" "IRQ" "ISL" "ITA" "JAM"
 [85] "JOR" "JPN" "KAZ" "KEN" "KGZ" "KHM" "KIR" "KOR" "KWT" "LAO" "LBN" "LBR"
 [97] "LBY" "LCA" "LKA" "LSO" "LTU" "LUX" "LVA" "MAC" "MAR" "MDA" "MDG" "MDV"
[109] "MEX" "MKD" "MLI" "MLT" "MMR" "MNE" "MNG" "MOZ" "MRT" "MUS" "MWI" "MYS"
[121] "NAM" "NCL" "NER" "NGA" "NIC" "NLD" "NOR" "NPL" "NZL" "OMN" "PAK" "PAN"
[133] "PER" "PHL" "PNG" "POL" "PRI" "PRT" "PRY" "PYF" "QAT" "ROU" "RUS" "RWA"
[145] "SAU" "SDN" "SEN" "SGP" "SLB" "SLE" "SLV" "SOM" "SSD" "STP" "SUR" "SVK"
[157] "SVN" "SWE" "SWZ" "SYR" "TCD" "TGO" "THA" "TJK" "TKM" "TLS" "TON" "TTO"
[169] "TUN" "TUR" "TZA" "UGA" "UKR" "URY" "USA" "UZB" "VCT" "VEN" "VIR" "VNM"
[181] "VUT" "WSM" "YEM" "ZAF" "COD" "ZMB" "ZWE"
> Life_Expectancy_At_Birth_1960
  [1] 65.56937 32.32851 32.98483 62.25437 52.24322 65.21554 65.86346 61.78273
  [9] 70.81707 68.58561 60.83624 41.23605 69.70195 37.27827 34.47790 45.82932
 [17] 69.24756 52.08937 62.72905 60.27622 67.70810 59.96137 42.11832 54.20546
 [25] 60.73805 62.50037 32.35937 50.54773 36.48263 71.13317 71.31341 57.45829
 [33] 43.46580 36.87241 41.52376 48.58163 56.71676 41.44244 48.85641 60.57620
 [41] 63.90466 69.59393 70.34878 69.31295 44.02127 72.17659 51.84527 46.13512
 [49] 53.21500 48.01371 37.36290 69.10927 67.90598 38.40571 68.81976 55.95849
 [57] 69.86829 57.58659 39.57012 71.12683 63.43185 45.83146 34.88639 32.04222
 [65] 37.84044 36.73305 68.16390 59.81593 45.53163 61.22634 60.27873 66.99971
 [73] 46.28832 64.60866 42.10010 68.00317 48.64032 41.17195 69.69176 44.94551
 [81] 48.03068 73.42866 69.12390 64.19183 52.68527 67.66610 58.36759 46.36241
 [89] 56.12807 41.23202 49.21598 53.00132 60.34795 43.20446 63.28012 34.78317
 [97] 42.64120 57.30376 59.74715 46.51071 69.84732 68.44639 69.78683 64.66093
[105] 48.44663 61.81278 39.97468 37.26863 57.06563 60.62280 28.21161 67.60178
[113] 42.73639 63.70561 48.36880 35.00371 43.48310 58.74522 37.77363 59.47534
[121] 46.88039 58.63902 35.51505 37.18295 46.99883 73.39268 73.54976 35.17083
[129] 71.23659 42.66707 45.29046 60.88171 47.69159 57.81193 38.46224 67.68049
[137] 68.71961 62.80893 63.79371 56.35705 61.20607 65.64244 66.05529 42.24929
[145] 45.66627 48.18763 38.20600 65.65983 49.38171 30.33154 49.94793 36.96588
[153] 31.67671 50.45137 59.68012 69.97593 68.97805 73.00561 44.23378 52.76824
[161] 38.01612 40.27283 54.69932 56.15354 54.45868 33.72712 61.36454 62.65759
[169] 42.00976 45.38441 43.65388 43.98356 68.29954 67.89639 69.77073 58.88554
[177] 57.72388 59.28512 63.73022 59.06702 46.48749 49.96951 34.36380 49.03629
[185] 41.01805 45.10980 51.54246
> Life_Expectancy_At_Birth_2013
  [1] 75.32866 60.02827 51.86617 77.53724 77.19563 75.98610 74.56137 75.77866
  [9] 82.19756 80.89024 70.69315 56.25161 80.38537 59.31202 58.24063 71.24524
 [17] 74.46585 76.54595 75.07354 76.27693 72.47073 69.98205 67.91344 74.12244
 [25] 75.33395 78.54666 69.10293 64.36080 49.87988 81.40112 82.74878 81.19793
 [33] 75.35302 51.20846 55.04180 61.66639 73.80973 62.93217 72.97237 79.22522
 [41] 79.25639 79.94978 78.27805 81.04390 61.68646 80.30244 73.31990 74.56895
 [49] 75.64851 70.92578 63.17788 82.42683 76.42439 63.44220 80.83171 69.91793
 [57] 81.96829 68.97339 63.84359 80.95610 74.07951 61.14207 58.21649 59.99927
 [65] 54.83841 57.29083 80.63415 73.19356 71.48639 78.87251 66.31002 83.83171
 [73] 72.94285 77.12683 62.40115 75.26829 68.70461 67.66041 81.04390 75.12598
 [81] 69.47168 83.11707 82.29024 73.46893 73.90141 83.33195 70.45000 60.95378
 [89] 70.20244 67.77205 65.76659 81.45976 74.46276 65.68724 80.12888 60.52039
 [97] 71.65768 74.91271 74.24029 49.33146 74.16341 81.79756 73.98049 80.33915
[105] 73.70905 68.81151 64.67390 76.60261 76.53266 75.18705 57.53520 80.74634
[113] 65.65410 74.75837 69.06180 54.64151 62.80271 74.46000 61.46600 74.56751
[121] 64.34388 77.12195 60.82815 52.44215 74.51476 81.10488 81.45122 69.22200
[129] 81.40732 76.84105 65.96368 77.41922 74.28385 68.13156 62.44917 76.84878
[137] 78.71120 80.37317 72.79917 76.33407 78.41849 74.46341 71.07317 63.39483
[145] 74.17763 63.16705 65.87876 82.34634 67.71893 50.36312 72.49815 55.02302
[153] 55.22090 66.25951 70.99000 76.26098 80.27805 81.70488 48.93793 74.71578
[161] 51.19149 59.13237 74.24693 69.40017 65.45656 67.52237 72.64034 70.30529
[169] 73.64634 75.17595 64.29183 57.76768 71.15951 76.83620 78.84146 68.22759
[177] 72.81088 74.07441 79.62439 75.75649 71.66924 73.25039 63.58351 56.73659
[185] 58.27193 59.23737 55.63300
I have no idea why I can't get it to work properly on my machine. Below is the result I obtained.

fixed: It works perfectly. Thanks a million
 
Last edited: