Struggling to filter data in R - r

Here is the data that I am using :
https://www.dropbox.com/s/dl/chmzqmus6bfoaim/climate_clean.csv
I added a variable called average_temperature_fahrenheit by doing so
climate = mutate(climate, average_temperature_fahrenheit = 9/5*average_temperature_celsius+32)
Now I want to know the highest temperature in Fahrenheit during the months of June, July and August between 1970 and 1980 in Europe and North America so I thought I needed to filter my data frame climate by doing so
climate %>% filter(continent == c("Europe","North America") & month == c("Jun","Jul","Aug") & year[1970:1980])
But clearly I did not succeed because it shows me only the month of August
Please could you tell me where I messed up in the filter function part

You could go with
library(data.table)
climate <-fread("~/downloads/climate_clean.csv")
climate %>%
mutate(average_temperature_fahrenheit = 9/5*average_temperature_celsius+32) %>%
filter(year %in% 1970:1980, month %in% c("Jun", "Jul", "Aug")) %>%
group_by(continent) %>%
summarise(MaxF = max(average_temperature_fahrenheit))
# A tibble: 6 x 2
continent MaxF
<chr> <dbl>
1 Africa 93.6
2 Asia 98.6
3 Europe 83.9
4 North America 83.0
5 Oceania 83.7
6 South America 80.2

You could reverse the rank function touse it in ave and subset the data where it's 1. Instead of creating a long spaghetti of code I'd do subsetting in multiple steps.
## subset by month, year, continent
res <- climate[climate$month %in% month.abb[6:8] & climate$year %in% 1970:1980 &
climate$continent %in% c("Europe", "North America"), ]
## create rank
res <- transform(res,
ave.temp.F=average_temperature_celsius * 9/5 + 32,
rank=ave(average_temperature_celsius, iso3,
FUN=function(x) rev(rank(x))))
## subset by rank
res <- res[res$rank == 1, ]
## some ordering (`-9` drops the rank column)
res <- res[order(res$continent, res$year, res$month, res$iso3), -9]
Result
res
# country iso3 continent year month average_temperature_celsius average_rainfall_mm ave.temp.F
# 177956 Italy ITA Europe 1970 Aug 20.41250 77.53980 68.74250
# 189836 Monaco MCO Europe 1970 Aug 17.54660 51.22330 63.58388
# 179535 Kazakhstan KAZ Europe 1971 Aug 19.82530 18.28780 67.68554
# 148959 Armenia ARM Europe 1972 Aug 19.71050 34.85870 67.47890
# 150399 Azerbaijan AZE Europe 1972 Aug 23.73650 18.82320 74.72570
# 206420 Spain ESP Europe 1972 Aug 20.46590 29.92810 68.83862
# 170559 Georgia GEO Europe 1972 Aug 19.18560 66.58620 66.53408
# 198500 Portugal PRT Europe 1972 Aug 20.93670 6.03864 69.68606
# 200799 Russia RUS Europe 1972 Aug 11.89720 61.69850 53.41496
# 146684 Albania ALB Europe 1974 Aug 21.45010 46.19520 70.61018
# 152444 Belarus BLR Europe 1974 Aug 17.00830 43.09090 62.61494
# 163047 Cyprus CYP Europe 1974 Aug 27.13730 3.16681 80.84714
# 167204 Estonia EST Europe 1974 Aug 15.27410 56.88330 59.49338
# 169004 Finland FIN Europe 1974 Aug 13.47990 91.12890 56.26382
# 171884 Greece GRC Europe 1974 Aug 23.14990 13.01370 73.66982
# 184844 Lithuania LTU Europe 1974 Aug 16.37240 53.21470 61.47032
# 182684 Latvia LVA Europe 1974 Aug 15.71120 59.34710 60.28016
# 185564 Macedonia MKD Europe 1974 Aug 20.34650 33.97170 68.62370
# 215084 Ukraine UKR Europe 1974 Aug 19.58960 39.45000 67.26128
# 156042 Bulgaria BGR Europe 1974 Jun 17.94660 66.96620 64.30388
# 154602 Bosnia and Herzegovina BIH Europe 1974 Jun 15.80810 128.16000 60.45458
# 189522 Moldova MDA Europe 1974 Jun 18.23260 87.43030 64.81868
# 199602 Republic of Montenegro MNE Europe 1974 Jun 14.96460 103.38300 58.93628
# 175496 Iceland ISL Europe 1975 Aug 8.40970 87.26150 47.13746
# 187736 Malta MLT Europe 1975 Aug 25.47840 36.46360 77.86112
# 194936 Norway NOR Europe 1975 Aug 11.25500 74.38320 52.25900
# 209336 Sweden SWE Europe 1975 Aug 13.22820 60.74740 55.81076
# 149948 Austria AUT Europe 1976 Aug 12.60120 98.79700 54.68216
# 163628 Czech Republic CZE Europe 1976 Aug 15.39740 55.42650 59.71532
# 162188 Croatia HRV Europe 1976 Aug 16.69060 98.21130 62.04308
# 175148 Hungary HUN Europe 1976 Aug 17.16470 48.66250 62.89646
# 198188 Poland POL Europe 1976 Aug 15.82960 48.73520 60.49328
# 200348 Romania ROU Europe 1976 Aug 16.03970 85.58330 60.87146
# 199988 Republic of Serbia SRB Europe 1976 Aug 16.82680 78.80250 62.28824
# 204308 Slovakia SVK Europe 1976 Aug 14.86390 53.49560 58.75502
# 204668 Slovenia SVN Europe 1976 Aug 14.87980 98.93020 58.78364
# 213519 Turkey TUR Europe 1977 Aug 22.37990 7.52567 72.28382
# 147452 Andorra AND Europe 1978 Aug 18.81170 33.16530 65.86106
# 152852 Belgium BEL Europe 1978 Aug 15.65460 39.78100 60.17828
# 169412 France FRA Europe 1978 Aug 17.11220 41.63730 62.80196
# 215852 United Kingdom GBR Europe 1978 Aug 13.77240 83.64290 56.79032
# 177332 Ireland IRL Europe 1978 Aug 13.97130 86.99350 57.14834
# 185252 Luxembourg LUX Europe 1978 Aug 15.21680 43.07880 59.39024
# 192452 Netherlands NLD Europe 1978 Aug 15.46520 48.70340 59.83736
# 209744 Switzerland CHE Europe 1979 Aug 12.49180 191.84800 54.48524
# 171224 Germany DEU Europe 1979 Aug 15.89550 71.42340 60.61190
# 164024 Denmark DNK Europe 1979 Aug 15.08630 80.27490 59.15534
# 167984 Faroe Islands FRO Europe 1979 Aug 9.45723 82.92610 49.02301
# 184544 Liechtenstein LIE Europe 1979 Aug 9.05277 217.23500 48.29499
# 172208 Greenland GRL North America 1971 Aug -6.22770 52.95410 20.79014
# 157820 Canada CAN North America 1972 Aug 9.42089 53.17810 48.95760
# 148124 Antigua and Barbuda ATG North America 1974 Aug 26.48220 174.62500 79.66796
# 151004 Bahamas BHS North America 1974 Aug 27.64530 149.01000 81.76154
# 162524 Cuba CUB North America 1974 Aug 27.07380 110.54600 80.73284
# 164684 Dominica DMA North America 1974 Aug 25.12810 145.65000 77.23058
# 165044 Dominican Republic DOM North America 1974 Aug 25.07960 143.50400 77.14328
# 174404 Haiti HTI North America 1974 Aug 25.46790 152.89300 77.84222
# 178364 Jamaica JAM North America 1974 Aug 25.69750 329.85900 78.25550
# 207164 St. Kitts and Nevis KNA North America 1974 Aug 25.65000 181.47400 78.17000
# 198884 Puerto Rico PRI North America 1974 Aug 26.26840 246.12400 79.28312
# 212564 Trinidad and Tobago TTO North America 1974 Aug 26.11950 182.57200 79.01510
# 216176 United States USA North America 1975 Aug 18.68970 64.20100 65.64146
# 196375 Panama PAN North America 1975 Jul 24.11360 325.95700 75.40448
# 161454 Costa Rica CRI North America 1975 Jun 23.90800 333.15800 75.03440
# 153223 Belize BLZ North America 1979 Jul 26.74570 349.16200 80.14226
# 172662 Grenada GRD North America 1979 Jun 27.77390 239.63500 81.99302
# 173022 Guatemala GTM North America 1979 Jun 24.49050 400.83000 76.08290
# 174822 Honduras HND North America 1979 Jun 24.59780 372.34600 76.27604
# 207582 St. Lucia LCA North America 1979 Jun 26.97770 335.70000 80.55986
# 189222 Mexico MEX North America 1979 Jun 24.40470 81.00740 75.92846
# 193542 Nicaragua NIC North America 1979 Jun 25.30880 466.90400 77.55584
# 166182 El Salvador SLV North America 1979 Jun 24.14100 348.93900 75.45380
# 207942 St. Vincent and the Grenadines VCT North America 1979 Jun 28.16000 252.66000 82.68800

Related

Counting number of consecutive years in a range in R

I need to count the number of contiguous years in a data frame. I want to filter data frames that have more than 30 years of consecutive records. Before I was doing:
(length(unique(Daily_Streamflow$year)) > 30
But I realized that the number of years (unique years) could be more than 30 but not in a consecutive range, for example:
(unique(DSF_09494000$year))
[1] 1917 1918 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
[27] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
[53] 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
How is possible to count the number of years in a range that is continuous without missing years? Is there a similar function as na.contiguous of stats package but for non-NA values?

How can I load the csv data into r? Especially without a file?

Tried to download the data from website but it gives this result. No download occurred.
Country Name,Country Code,Region,Year,Fertility Rate
Aruba,ABW,The Americas,1960,4.82
Afghanistan,AFG,Asia,1960,7.45
Angola,AGO,Africa,1960,7.379
Albania,ALB,Europe,1960,6.186
United Arab Emirates,ARE,Middle East,1960,6.928
Argentina,ARG,The Americas,1960,3.109
Armenia,ARM,Asia,1960,4.55
Antigua and Barbuda,ATG,The Americas,1960,4.425
Australia,AUS,Oceania,1960,3.453
Austria,AUT,Europe,1960,2.69
Azerbaijan,AZE,Asia,1960,5.571
Burundi,BDI,Africa,1960,6.953
Belgium,BEL,Europe,1960,2.54
Benin,BEN,Africa,1960,6.282
Burkina Faso,BFA,Africa,1960,6.291
Bangladesh,BGD,Asia,1960,6.725
Bulgaria,BGR,Europe,1960,2.31
Bahrain,BHR,Middle East,1960,7.09
"Bahamas, The",BHS,The Americas,1960,4.495
Bosnia and Herzegovina,BIH,Europe,1960,3.77
Belarus,BLR,Europe,1960,2.67
Belize,BLZ,The Americas,1960,6.5
Bolivia,BOL,The Americas,1960,6.7
Brazil,BRA,The Americas,1960,6.21
Barbados,BRB,The Americas,1960,4.333
Brunei Darussalam,BRN,Asia,1960,6.487
Bhutan,BTN,Asia,1960,6.67
Botswana,BWA,Africa,1960,6.615
Central African Republic,CAF,Africa,1960,5.84
Canada,CAN,The Americas,1960,3.811
Switzerland,CHE,Europe,1960,2.44
Chile,CHL,The Americas,1960,5.113
China,CHN,Asia,1960,5.758
Cote d'Ivoire,CIV,Africa,1960,7.351
Cameroon,CMR,Africa,1960,5.647
"Congo, Rep.",COG,Africa,1960,5.88
Colombia,COL,The Americas,1960,6.807
Comoros,COM,Africa,1960,6.792
Cabo Verde,CPV,Africa,1960,6.885
Costa Rica,CRI,The Americas,1960,6.451
Cuba,CUB,The Americas,1960,4.182
Cyprus,CYP,Europe,1960,3.5
Czech Republic,CZE,Europe,1960,2.09
Germany,DEU,Europe,1960,2.37
Djibouti,DJI,Africa,1960,6.461
Denmark,DNK,Europe,1960,2.57
Dominican Republic,DOM,The Americas,1960,7.555
Algeria,DZA,Africa,1960,7.524
Ecuador,ECU,The Americas,1960,6.721
"Egypt, Arab Rep.",EGY,Africa,1960,6.63
Eritrea,ERI,Africa,1960,6.899
Spain,ESP,Europe,1960,2.86
Estonia,EST,Europe,1960,1.98
Ethiopia,ETH,Africa,1960,6.88
Finland,FIN,Europe,1960,2.72
Fiji,FJI,Oceania,1960,6.461
France,FRA,Europe,1960,2.85
"Micronesia, Fed. Sts.",FSM,Oceania,1960,6.934
Gabon,GAB,Africa,1960,4.384
United Kingdom,GBR,Europe,1960,2.69
Georgia,GEO,Asia,1960,2.964
Ghana,GHA,Africa,1960,6.749
Guinea,GIN,Africa,1960,6.114
"Gambia, The",GMB,Africa,1960,5.573
Guinea-Bissau,GNB,Africa,1960,5.921
Equatorial Guinea,GNQ,Africa,1960,5.505
Greece,GRC,Europe,1960,2.23
Grenada,GRD,The Americas,1960,6.743
Guatemala,GTM,The Americas,1960,6.534
Guam,GUM,Oceania,1960,6.052
Guyana,GUY,The Americas,1960,5.754
"Hong Kong SAR, China",HKG,Asia,1960,5.162
Honduras,HND,The Americas,1960,7.458
Croatia,HRV,Europe,1960,2.332
Haiti,HTI,The Americas,1960,6.324
Hungary,HUN,Europe,1960,2.02
Indonesia,IDN,Asia,1960,5.666
India,IND,Asia,1960,5.906
Ireland,IRL,Europe,1960,3.78
"Iran, Islamic Rep.",IRN,Middle East,1960,6.927
Iraq,IRQ,Middle East,1960,6.252
Iceland,ISL,Europe,1960,4.29
Italy,ITA,Europe,1960,2.37
Jamaica,JAM,The Americas,1960,5.419
Jordan,JOR,Middle East,1960,7.687
Japan,JPN,Asia,1960,2.001
Kazakhstan,KAZ,Asia,1960,4.563
Kenya,KEN,Africa,1960,7.946
Kyrgyz Republic,KGZ,Asia,1960,5.166
Cambodia,KHM,Asia,1960,6.967
Kiribati,KIR,Oceania,1960,6.788
"Korea, Rep.",KOR,Asia,1960,6.155
Kuwait,KWT,Middle East,1960,7.251
Lao PDR,LAO,Asia,1960,5.961
Lebanon,LBN,Middle East,1960,5.739
Liberia,LBR,Africa,1960,6.406
Libya,LBY,Africa,1960,7.202
St. Lucia,LCA,The Americas,1960,6.967
Sri Lanka,LKA,Asia,1960,5.541
Lesotho,LSO,Africa,1960,5.839
Lithuania,LTU,Europe,1960,2.56
Luxembourg,LUX,Europe,1960,2.29
Latvia,LVA,Europe,1960,1.94
"Macao SAR, China",MAC,Asia,1960,4.945
Morocco,MAR,Africa,1960,7.065
Moldova,MDA,Europe,1960,3.328
Madagascar,MDG,Africa,1960,7.3
Maldives,MDV,Asia,1960,7.021
Mexico,MEX,The Americas,1960,6.768
"Macedonia, FYR",MKD,Europe,1960,3.842
Mali,MLI,Africa,1960,6.967
Malta,MLT,Europe,1960,3.62
Myanmar,MMR,Asia,1960,6.051
Montenegro,MNE,Europe,1960,3.521
Mongolia,MNG,Asia,1960,6.953
Mozambique,MOZ,Africa,1960,6.6
Mauritania,MRT,Africa,1960,6.775
Mauritius,MUS,Africa,1960,6.167
Malawi,MWI,Africa,1960,6.94
Malaysia,MYS,Asia,1960,6.194
Namibia,NAM,Africa,1960,6.15
New Caledonia,NCL,Oceania,1960,6.278
Niger,NER,Africa,1960,7.407
Nigeria,NGA,Africa,1960,6.354
Nicaragua,NIC,The Americas,1960,7.336
Netherlands,NLD,Europe,1960,3.12
Norway,NOR,Europe,1960,2.85
Nepal,NPL,Asia,1960,5.959
New Zealand,NZL,Oceania,1960,4.129
Oman,OMN,Middle East,1960,7.247
Pakistan,PAK,Asia,1960,6.6
Panama,PAN,The Americas,1960,5.87
Peru,PER,The Americas,1960,6.971
Philippines,PHL,Asia,1960,7.148
Papua New Guinea,PNG,Oceania,1960,6.275
Poland,POL,Europe,1960,2.98
Puerto Rico,PRI,The Americas,1960,4.657
Portugal,PRT,Europe,1960,3.16
Paraguay,PRY,The Americas,1960,6.5
French Polynesia,PYF,Oceania,1960,5.658
Qatar,QAT,Middle East,1960,6.971
Romania,ROU,Europe,1960,2.34
Russian Federation,RUS,Europe,1960,2.52
Rwanda,RWA,Africa,1960,8.187
Saudi Arabia,SAU,Middle East,1960,7.216
Sudan,SDN,Africa,1960,6.691
Senegal,SEN,Africa,1960,6.951
Singapore,SGP,Asia,1960,5.454
Solomon Islands,SLB,Oceania,1960,6.388
Sierra Leone,SLE,Africa,1960,5.97
El Salvador,SLV,The Americas,1960,6.674
Somalia,SOM,Africa,1960,7.25
South Sudan,SSD,Africa,1960,6.721
Sao Tome and Principe,STP,Africa,1960,6.242
Suriname,SUR,The Americas,1960,6.608
Slovak Republic,SVK,Europe,1960,3.04
Slovenia,SVN,Europe,1960,2.324
Sweden,SWE,Europe,1960,2.17
Swaziland,SWZ,Africa,1960,6.717
Syrian Arab Republic,SYR,Middle East,1960,7.467
Chad,TCD,Africa,1960,6.25
Togo,TGO,Africa,1960,6.521
Thailand,THA,Asia,1960,6.147
Tajikistan,TJK,Asia,1960,6.235
Turkmenistan,TKM,Asia,1960,6.415
Timor-Leste,TLS,Asia,1960,6.373
Tonga,TON,Oceania,1960,7.363
Trinidad and Tobago,TTO,The Americas,1960,5.264
Tunisia,TUN,Africa,1960,7.043
Turkey,TUR,Europe,1960,6.303
Tanzania,TZA,Africa,1960,6.806
Uganda,UGA,Africa,1960,6.999
Ukraine,UKR,Europe,1960,2.24
Uruguay,URY,The Americas,1960,2.88
United States,USA,The Americas,1960,3.654
Uzbekistan,UZB,Asia,1960,6.707
St. Vincent and the Grenadines,VCT,The Americas,1960,7.224
"Venezuela, RB",VEN,The Americas,1960,6.616
Virgin Islands (U.S.),VIR,The Americas,1960,5.615
Vietnam,VNM,Asia,1960,6.348
Vanuatu,VUT,Oceania,1960,7.197
Samoa,WSM,Oceania,1960,7.651
"Yemen, Rep.",YEM,Middle East,1960,7.38
South Africa,ZAF,Africa,1960,6.173
"Congo, Dem. Rep.",COD,Africa,1960,6.001
Zambia,ZMB,Africa,1960,7.018
Zimbabwe,ZWE,Africa,1960,7.158
Aruba,ABW,The Americas,2013,1.669
Afghanistan,AFG,Asia,2013,5.05
Angola,AGO,Africa,2013,6.165
Albania,ALB,Europe,2013,1.771
United Arab Emirates,ARE,Middle East,2013,1.801
Argentina,ARG,The Americas,2013,2.335
Armenia,ARM,Asia,2013,1.553
Antigua and Barbuda,ATG,The Americas,2013,2.088
Australia,AUS,Oceania,2013,1.921
Austria,AUT,Europe,2013,1.44
Azerbaijan,AZE,Asia,2013,2
Burundi,BDI,Africa,2013,6.035
Belgium,BEL,Europe,2013,1.79
Benin,BEN,Africa,2013,4.846
Burkina Faso,BFA,Africa,2013,5.607
Bangladesh,BGD,Asia,2013,2.209
Bulgaria,BGR,Europe,2013,1.5
Bahrain,BHR,Middle East,2013,2.075
"Bahamas, The",BHS,The Americas,2013,1.883
Bosnia and Herzegovina,BIH,Europe,2013,1.272
Belarus,BLR,Europe,2013,1.62
Belize,BLZ,The Americas,2013,2.611
Bolivia,BOL,The Americas,2013,3.017
Brazil,BRA,The Americas,2013,1.801
Barbados,BRB,The Americas,2013,1.791
Brunei Darussalam,BRN,Asia,2013,1.893
Bhutan,BTN,Asia,2013,2.082
Botswana,BWA,Africa,2013,2.864
Central African Republic,CAF,Africa,2013,4.368
Canada,CAN,The Americas,2013,1.61
Switzerland,CHE,Europe,2013,1.52
Chile,CHL,The Americas,2013,1.774
China,CHN,Asia,2013,1.668
Cote d'Ivoire,CIV,Africa,2013,5.063
Cameroon,CMR,Africa,2013,4.781
"Congo, Rep.",COG,Africa,2013,4.919
Colombia,COL,The Americas,2013,1.922
Comoros,COM,Africa,2013,4.56
Cabo Verde,CPV,Africa,2013,2.339
Costa Rica,CRI,The Americas,2013,1.841
Cuba,CUB,The Americas,2013,1.622
Cyprus,CYP,Europe,2013,1.455
Czech Republic,CZE,Europe,2013,1.45
Germany,DEU,Europe,2013,1.38
Djibouti,DJI,Africa,2013,3.262
Denmark,DNK,Europe,2013,1.73
Dominican Republic,DOM,The Americas,2013,2.51
Algeria,DZA,Africa,2013,2.893
Ecuador,ECU,The Americas,2013,2.571
"Egypt, Arab Rep.",EGY,Africa,2013,3.336
Eritrea,ERI,Africa,2013,4.363
Spain,ESP,Europe,2013,1.32
Estonia,EST,Europe,2013,1.56
Ethiopia,ETH,Africa,2013,4.517
Finland,FIN,Europe,2013,1.8
Fiji,FJI,Oceania,2013,2.589
France,FRA,Europe,2013,2.01
"Micronesia, Fed. Sts.",FSM,Oceania,2013,3.294
Gabon,GAB,Africa,2013,3.963
United Kingdom,GBR,Europe,2013,1.92
Georgia,GEO,Asia,2013,1.817
Ghana,GHA,Africa,2013,4.209
Guinea,GIN,Africa,2013,5.094
"Gambia, The",GMB,Africa,2013,5.751
Guinea-Bissau,GNB,Africa,2013,4.906
Equatorial Guinea,GNQ,Africa,2013,4.924
Greece,GRC,Europe,2013,1.29
Grenada,GRD,The Americas,2013,2.171
Guatemala,GTM,The Americas,2013,3.263
Guam,GUM,Oceania,2013,2.406
Guyana,GUY,The Americas,2013,2.585
"Hong Kong SAR, China",HKG,Asia,2013,1.124
Honduras,HND,The Americas,2013,2.442
Croatia,HRV,Europe,2013,1.51
Haiti,HTI,The Americas,2013,3.098
Hungary,HUN,Europe,2013,1.34
Indonesia,IDN,Asia,2013,2.484
India,IND,Asia,2013,2.465
Ireland,IRL,Europe,2013,2.01
"Iran, Islamic Rep.",IRN,Middle East,2013,1.726
Iraq,IRQ,Middle East,2013,4.026
Iceland,ISL,Europe,2013,2.04
Italy,ITA,Europe,2013,1.43
Jamaica,JAM,The Americas,2013,2.26
Jordan,JOR,Middle East,2013,3.244
Japan,JPN,Asia,2013,1.43
Kazakhstan,KAZ,Asia,2013,2.64
Kenya,KEN,Africa,2013,4.406
Kyrgyz Republic,KGZ,Asia,2013,3.2
Cambodia,KHM,Asia,2013,2.683
Kiribati,KIR,Oceania,2013,3.765
"Korea, Rep.",KOR,Asia,2013,1.187
Kuwait,KWT,Middle East,2013,2.6
Lao PDR,LAO,Asia,2013,3.063
Lebanon,LBN,Middle East,2013,1.495
Liberia,LBR,Africa,2013,4.793
Libya,LBY,Africa,2013,2.507
St. Lucia,LCA,The Americas,2013,1.913
Sri Lanka,LKA,Asia,2013,2.339
Lesotho,LSO,Africa,2013,3.222
Lithuania,LTU,Europe,2013,1.6
Luxembourg,LUX,Europe,2013,1.57
Latvia,LVA,Europe,2013,1.44
"Macao SAR, China",MAC,Asia,2013,1.204
Morocco,MAR,Africa,2013,2.535
Moldova,MDA,Europe,2013,1.456
Madagascar,MDG,Africa,2013,4.468
Maldives,MDV,Asia,2013,2.152
Mexico,MEX,The Americas,2013,2.272
"Macedonia, FYR",MKD,Europe,2013,1.511
Mali,MLI,Africa,2013,6.314
Malta,MLT,Europe,2013,1.43
Myanmar,MMR,Asia,2013,2.239
Montenegro,MNE,Europe,2013,1.666
Mongolia,MNG,Asia,2013,2.657
Mozambique,MOZ,Africa,2013,5.418
Mauritania,MRT,Africa,2013,4.662
Mauritius,MUS,Africa,2013,1.44
Malawi,MWI,Africa,2013,5.22
Malaysia,MYS,Asia,2013,1.956
Namibia,NAM,Africa,2013,3.56
New Caledonia,NCL,Oceania,2013,2.28
Niger,NER,Africa,2013,7.623
Nigeria,NGA,Africa,2013,5.707
Nicaragua,NIC,The Americas,2013,2.3
Netherlands,NLD,Europe,2013,1.72
Norway,NOR,Europe,2013,1.85
Nepal,NPL,Asia,2013,2.292
New Zealand,NZL,Oceania,2013,1.95
Oman,OMN,Middle East,2013,2.823
Pakistan,PAK,Asia,2013,3.682
Panama,PAN,The Americas,2013,2.465
Peru,PER,The Americas,2013,2.48
Philippines,PHL,Asia,2013,3.011
Papua New Guinea,PNG,Oceania,2013,3.812
Poland,POL,Europe,2013,1.3
Puerto Rico,PRI,The Americas,2013,1.636
Portugal,PRT,Europe,2013,1.28
Paraguay,PRY,The Americas,2013,2.581
French Polynesia,PYF,Oceania,2013,2.059
Qatar,QAT,Middle East,2013,2.043
Romania,ROU,Europe,2013,1.53
Russian Federation,RUS,Europe,2013,1.7
Rwanda,RWA,Africa,2013,4.012
Saudi Arabia,SAU,Middle East,2013,2.818
Sudan,SDN,Africa,2013,4.421
Senegal,SEN,Africa,2013,5.134
Singapore,SGP,Asia,2013,1.19
Solomon Islands,SLB,Oceania,2013,4.031
Sierra Leone,SLE,Africa,2013,4.746
El Salvador,SLV,The Americas,2013,1.958
Somalia,SOM,Africa,2013,6.565
South Sudan,SSD,Africa,2013,5.108
Sao Tome and Principe,STP,Africa,2013,4.634
Suriname,SUR,The Americas,2013,2.386
Slovak Republic,SVK,Europe,2013,1.34
Slovenia,SVN,Europe,2013,1.58
Sweden,SWE,Europe,2013,1.91
Swaziland,SWZ,Africa,2013,3.334
Syrian Arab Republic,SYR,Middle East,2013,2.964
Chad,TCD,Africa,2013,6.264
Togo,TGO,Africa,2013,4.655
Thailand,THA,Asia,2013,1.524
Tajikistan,TJK,Asia,2013,3.514
Turkmenistan,TKM,Asia,2013,2.326
Timor-Leste,TLS,Asia,2013,5.2
Tonga,TON,Oceania,2013,3.768
Trinidad and Tobago,TTO,The Americas,2013,1.789
Tunisia,TUN,Africa,2013,2.25
Turkey,TUR,Europe,2013,2.041
Tanzania,TZA,Africa,2013,5.215
Uganda,UGA,Africa,2013,5.869
Ukraine,UKR,Europe,2013,1.506
Uruguay,URY,The Americas,2013,2.033
United States,USA,The Americas,2013,1.8695
Uzbekistan,UZB,Asia,2013,2.2
St. Vincent and the Grenadines,VCT,The Americas,2013,1.997
"Venezuela, RB",VEN,The Americas,2013,2.391
Virgin Islands (U.S.),VIR,The Americas,2013,1.77
Vietnam,VNM,Asia,2013,1.743
Vanuatu,VUT,Oceania,2013,3.383
Samoa,WSM,Oceania,2013,4.147
"Yemen, Rep.",YEM,Middle East,2013,4.284
South Africa,ZAF,Africa,2013,2.387
"Congo, Dem. Rep.",COD,Africa,2013,6.103
Zambia,ZMB,Africa,2013,5.429
Zimbabwe,ZWE,Africa,2013,3.977
If you don't want to save to a file, select all the text and copy it to your clipboard. For example, I just selected and copied the data in your question. Then in R you can do:
df <- read.csv(file = "clipboard")
So now df is the following data frame:
df
Country.Name Country.Code Region Year Fertility.Rate
1 Aruba ABW The Americas 1960 4.820
2 Afghanistan AFG Asia 1960 7.450
3 Angola AGO Africa 1960 7.379
4 Albania ALB Europe 1960 6.186
5 United Arab Emirates ARE Middle East 1960 6.928
6 Argentina ARG The Americas 1960 3.109
7 Armenia ARM Asia 1960 4.550
8 Antigua and Barbuda ATG The Americas 1960 4.425
9 Australia AUS Oceania 1960 3.453
10 Austria AUT Europe 1960 2.690
11 Azerbaijan AZE Asia 1960 5.571
12 Burundi BDI Africa 1960 6.953
13 Belgium BEL Europe 1960 2.540
14 Benin BEN Africa 1960 6.282
15 Burkina Faso BFA Africa 1960 6.291
16 Bangladesh BGD Asia 1960 6.725
17 Bulgaria BGR Europe 1960 2.310
18 Bahrain BHR Middle East 1960 7.090
19 Bahamas, The BHS The Americas 1960 4.495
20 Bosnia and Herzegovina BIH Europe 1960 3.770
21 Belarus BLR Europe 1960 2.670
22 Belize BLZ The Americas 1960 6.500
23 Bolivia BOL The Americas 1960 6.700
24 Brazil BRA The Americas 1960 6.210
25 Barbados BRB The Americas 1960 4.333
26 Brunei Darussalam BRN Asia 1960 6.487
27 Bhutan BTN Asia 1960 6.670
28 Botswana BWA Africa 1960 6.615
29 Central African Republic CAF Africa 1960 5.840
30 Canada CAN The Americas 1960 3.811
31 Switzerland CHE Europe 1960 2.440
32 Chile CHL The Americas 1960 5.113
33 China CHN Asia 1960 5.758
34 Cote d'Ivoire CIV Africa 1960 7.351
35 Cameroon CMR Africa 1960 5.647
36 Congo, Rep. COG Africa 1960 5.880
37 Colombia COL The Americas 1960 6.807
38 Comoros COM Africa 1960 6.792
39 Cabo Verde CPV Africa 1960 6.885
40 Costa Rica CRI The Americas 1960 6.451
41 Cuba CUB The Americas 1960 4.182
42 Cyprus CYP Europe 1960 3.500
43 Czech Republic CZE Europe 1960 2.090
44 Germany DEU Europe 1960 2.370
45 Djibouti DJI Africa 1960 6.461
46 Denmark DNK Europe 1960 2.570
47 Dominican Republic DOM The Americas 1960 7.555
48 Algeria DZA Africa 1960 7.524
49 Ecuador ECU The Americas 1960 6.721
50 Egypt, Arab Rep. EGY Africa 1960 6.630
51 Eritrea ERI Africa 1960 6.899
52 Spain ESP Europe 1960 2.860
53 Estonia EST Europe 1960 1.980
54 Ethiopia ETH Africa 1960 6.880
55 Finland FIN Europe 1960 2.720
56 Fiji FJI Oceania 1960 6.461
57 France FRA Europe 1960 2.850
58 Micronesia, Fed. Sts. FSM Oceania 1960 6.934
59 Gabon GAB Africa 1960 4.384
60 United Kingdom GBR Europe 1960 2.690
61 Georgia GEO Asia 1960 2.964
62 Ghana GHA Africa 1960 6.749
63 Guinea GIN Africa 1960 6.114
64 Gambia, The GMB Africa 1960 5.573
65 Guinea-Bissau GNB Africa 1960 5.921
66 Equatorial Guinea GNQ Africa 1960 5.505
67 Greece GRC Europe 1960 2.230
68 Grenada GRD The Americas 1960 6.743
69 Guatemala GTM The Americas 1960 6.534
70 Guam GUM Oceania 1960 6.052
71 Guyana GUY The Americas 1960 5.754
72 Hong Kong SAR, China HKG Asia 1960 5.162
73 Honduras HND The Americas 1960 7.458
74 Croatia HRV Europe 1960 2.332
75 Haiti HTI The Americas 1960 6.324
76 Hungary HUN Europe 1960 2.020
77 Indonesia IDN Asia 1960 5.666
78 India IND Asia 1960 5.906
79 Ireland IRL Europe 1960 3.780
80 Iran, Islamic Rep. IRN Middle East 1960 6.927
81 Iraq IRQ Middle East 1960 6.252
82 Iceland ISL Europe 1960 4.290
83 Italy ITA Europe 1960 2.370
84 Jamaica JAM The Americas 1960 5.419
85 Jordan JOR Middle East 1960 7.687
86 Japan JPN Asia 1960 2.001
87 Kazakhstan KAZ Asia 1960 4.563
88 Kenya KEN Africa 1960 7.946
89 Kyrgyz Republic KGZ Asia 1960 5.166
90 Cambodia KHM Asia 1960 6.967
91 Kiribati KIR Oceania 1960 6.788
92 Korea, Rep. KOR Asia 1960 6.155
93 Kuwait KWT Middle East 1960 7.251
94 Lao PDR LAO Asia 1960 5.961
95 Lebanon LBN Middle East 1960 5.739
96 Liberia LBR Africa 1960 6.406
97 Libya LBY Africa 1960 7.202
98 St. Lucia LCA The Americas 1960 6.967
99 Sri Lanka LKA Asia 1960 5.541
100 Lesotho LSO Africa 1960 5.839
101 Lithuania LTU Europe 1960 2.560
102 Luxembourg LUX Europe 1960 2.290
103 Latvia LVA Europe 1960 1.940
104 Macao SAR, China MAC Asia 1960 4.945
105 Morocco MAR Africa 1960 7.065
106 Moldova MDA Europe 1960 3.328
107 Madagascar MDG Africa 1960 7.300
108 Maldives MDV Asia 1960 7.021
109 Mexico MEX The Americas 1960 6.768
110 Macedonia, FYR MKD Europe 1960 3.842
111 Mali MLI Africa 1960 6.967
112 Malta MLT Europe 1960 3.620
113 Myanmar MMR Asia 1960 6.051
114 Montenegro MNE Europe 1960 3.521
115 Mongolia MNG Asia 1960 6.953
116 Mozambique MOZ Africa 1960 6.600
117 Mauritania MRT Africa 1960 6.775
118 Mauritius MUS Africa 1960 6.167
119 Malawi MWI Africa 1960 6.940
120 Malaysia MYS Asia 1960 6.194
121 Namibia NAM Africa 1960 6.150
122 New Caledonia NCL Oceania 1960 6.278
123 Niger NER Africa 1960 7.407
124 Nigeria NGA Africa 1960 6.354
125 Nicaragua NIC The Americas 1960 7.336
126 Netherlands NLD Europe 1960 3.120
127 Norway NOR Europe 1960 2.850
128 Nepal NPL Asia 1960 5.959
129 New Zealand NZL Oceania 1960 4.129
130 Oman OMN Middle East 1960 7.247
131 Pakistan PAK Asia 1960 6.600
132 Panama PAN The Americas 1960 5.870
133 Peru PER The Americas 1960 6.971
134 Philippines PHL Asia 1960 7.148
135 Papua New Guinea PNG Oceania 1960 6.275
136 Poland POL Europe 1960 2.980
137 Puerto Rico PRI The Americas 1960 4.657
138 Portugal PRT Europe 1960 3.160
139 Paraguay PRY The Americas 1960 6.500
140 French Polynesia PYF Oceania 1960 5.658
141 Qatar QAT Middle East 1960 6.971
142 Romania ROU Europe 1960 2.340
143 Russian Federation RUS Europe 1960 2.520
144 Rwanda RWA Africa 1960 8.187
145 Saudi Arabia SAU Middle East 1960 7.216
146 Sudan SDN Africa 1960 6.691
147 Senegal SEN Africa 1960 6.951
148 Singapore SGP Asia 1960 5.454
149 Solomon Islands SLB Oceania 1960 6.388
150 Sierra Leone SLE Africa 1960 5.970
151 El Salvador SLV The Americas 1960 6.674
152 Somalia SOM Africa 1960 7.250
153 South Sudan SSD Africa 1960 6.721
154 Sao Tome and Principe STP Africa 1960 6.242
155 Suriname SUR The Americas 1960 6.608
156 Slovak Republic SVK Europe 1960 3.040
157 Slovenia SVN Europe 1960 2.324
158 Sweden SWE Europe 1960 2.170
159 Swaziland SWZ Africa 1960 6.717
160 Syrian Arab Republic SYR Middle East 1960 7.467
161 Chad TCD Africa 1960 6.250
162 Togo TGO Africa 1960 6.521
163 Thailand THA Asia 1960 6.147
164 Tajikistan TJK Asia 1960 6.235
165 Turkmenistan TKM Asia 1960 6.415
166 Timor-Leste TLS Asia 1960 6.373
167 Tonga TON Oceania 1960 7.363
168 Trinidad and Tobago TTO The Americas 1960 5.264
169 Tunisia TUN Africa 1960 7.043
170 Turkey TUR Europe 1960 6.303
171 Tanzania TZA Africa 1960 6.806
172 Uganda UGA Africa 1960 6.999
173 Ukraine UKR Europe 1960 2.240
174 Uruguay URY The Americas 1960 2.880
175 United States USA The Americas 1960 3.654
176 Uzbekistan UZB Asia 1960 6.707
177 St. Vincent and the Grenadines VCT The Americas 1960 7.224
178 Venezuela, RB VEN The Americas 1960 6.616
179 Virgin Islands (U.S.) VIR The Americas 1960 5.615
180 Vietnam VNM Asia 1960 6.348
181 Vanuatu VUT Oceania 1960 7.197
182 Samoa WSM Oceania 1960 7.651
183 Yemen, Rep. YEM Middle East 1960 7.380
184 South Africa ZAF Africa 1960 6.173
185 Congo, Dem. Rep. COD Africa 1960 6.001
186 Zambia ZMB Africa 1960 7.018
187 Zimbabwe ZWE Africa 1960 7.158
188 Aruba ABW The Americas 2013 1.669
189 Afghanistan AFG Asia 2013 5.050
190 Angola AGO Africa 2013 6.165
191 Albania ALB Europe 2013 1.771
192 United Arab Emirates ARE Middle East 2013 1.801
193 Argentina ARG The Americas 2013 2.335
194 Armenia ARM Asia 2013 1.553
195 Antigua and Barbuda ATG The Americas 2013 2.088
196 Australia AUS Oceania 2013 1.921
197 Austria AUT Europe 2013 1.440
198 Azerbaijan AZE Asia 2013 2.000
199 Burundi BDI Africa 2013 6.035
200 Belgium BEL Europe 2013 1.790
We can use soread from overflow package after selecting and copying the file
library(overflow)
df1 <- soread(sep=",")
head(df1)
# Country.Name Country.Code Region Year Fertility.Rate
#1 Aruba ABW The Americas 1960 4.820
#2 Afghanistan AFG Asia 1960 7.450
#3 Angola AGO Africa 1960 7.379
#4 Albania ALB Europe 1960 6.186
#5 United Arab Emirates ARE Middle East 1960 6.928
#6 Argentina ARG The Americas 1960 3.109

Creating panel data, filling gaps between years and repeating the last value in the subsequent years using R

I have the following sample:
I am trying to turn it into the following panel data:
As you can see in the last image, I would like to repeat the values between years for the same country, and repeat the last value for the subsequent years until the year 2020.
You can use grid.expand to get the country / year combinations you want, then left_join the main data frame to this, and finally fill the missing data, ensuring you filter out any remaining NAs.
library(dplyr)
library(tidyr)
panel <- expand.grid(year = min(df$year):2020,
country = unique(df$country),
stringsAsFactors = FALSE) %>%
left_join(df) %>%
group_by(country) %>%
fill(c("id", "regioncode", "prespowl")) %>%
filter(!is.na(id)) %>%
as.data.frame()
Which gives the following result:
panel
#> year country id regioncode prespowl
#> 1 2011 Albania 1 Europe 0.1817557
#> 2 2012 Albania 1 Europe 0.1817557
#> 3 2013 Albania 1 Europe 0.1817557
#> 4 2014 Albania 1 Europe 0.1817557
#> 5 2015 Albania 1 Europe 0.1817557
#> 6 2016 Albania 1 Europe 0.1817557
#> 7 2017 Albania 1 Europe 0.1817557
#> 8 2018 Albania 1 Europe 0.1411482
#> 9 2019 Albania 1 Europe 0.1411482
#> 10 2020 Albania 1 Europe 0.1411482
#> 11 2016 Algeria 2 Africa 0.3837466
#> 12 2017 Algeria 2 Africa 0.3837466
#> 13 2018 Algeria 2 Africa 0.4837466
#> 14 2019 Algeria 2 Africa 0.4837466
#> 15 2020 Algeria 2 Africa 0.4837466
#> 16 1999 Argentina 3 Americas 0.2887138
#> 17 2000 Argentina 3 Americas 0.2887138
#> 18 2001 Argentina 3 Americas 0.2887138
#> 19 2002 Argentina 3 Americas 0.2887138
#> 20 2003 Argentina 3 Americas 0.2887138
#> 21 2004 Argentina 3 Americas 0.2887138
#> 22 2005 Argentina 3 Americas 0.2887138
#> 23 2006 Argentina 3 Americas 0.4322523
#> 24 2007 Argentina 3 Americas 0.4322523
#> 25 2008 Argentina 3 Americas 0.4322523
#> 26 2009 Argentina 3 Americas 0.4322523
#> 27 2010 Argentina 3 Americas 0.4322523
#> 28 2011 Argentina 3 Americas 0.4322523
#> 29 2012 Argentina 3 Americas 0.4322523
#> 30 2013 Argentina 3 Americas 0.5453171
#> 31 2014 Argentina 3 Americas 0.5453171
#> 32 2015 Argentina 3 Americas 0.5453171
#> 33 2016 Argentina 3 Americas 0.5453171
#> 34 2017 Argentina 3 Americas 0.5453171
#> 35 2018 Argentina 3 Americas 0.5453171
#> 36 2019 Argentina 3 Americas 0.5453171
#> 37 2020 Argentina 3 Americas 0.5453171
Data used:
df <- read.table(text= 'country year id regioncode prespowl
Albania 2011 1 Europe 0.1817557
Albania 2018 1 Europe 0.1411482
Algeria 2016 2 Africa 0.3837466
Algeria 2018 2 Africa 0.4837466
Argentina 1999 3 Americas 0.2887138
Argentina 2006 3 Americas 0.4322523
Argentina 2013 3 Americas 0.5453171
', header = TRUE, stringsAsFactors = FALSE)

I have a dataset with columns: c(country, element, 1961, 1961flags, 1962, 1962 flags)

That goes on for until 2011. I know that 1961:2011 would assign all years in between, but is there a way to accommodate the separate flag column?
To be more specific, it is a csv file. I am reading in the data as
data <-read.csv("file",
col.names=(country, element, *would be 1961:2011 if there were no flag columns*), header=True)
You can use paste0 to generate the names.
col.names = c(country, element, paste0(rep(1961:2011, each=2), c("", "flags")))
the rep call will generate:
[1] 1961 1961 1962 1962 1963 1963 1964 1964 1965 1965 1966 1966 1967 1967 1968
[16] 1968 1969 1969 1970 1970 1971 1971 1972 1972 1973 1973 1974 1974 1975 1975
[31] 1976 1976 1977 1977 1978 1978 1979 1979 1980 1980 1981 1981 1982 1982 1983
[46] 1983 1984 1984 1985 1985 1986 1986 1987 1987 1988 1988 1989 1989 1990 1990
[61] 1991 1991 1992 1992 1993 1993 1994 1994 1995 1995 1996 1996 1997 1997 1998
[76] 1998 1999 1999 2000 2000 2001 2001 2002 2002 2003 2003 2004 2004 2005 2005
[91] 2006 2006 2007 2007 2008 2008 2009 2009 2010 2010 2011 2011
Note that I am using each and not times which would instead result in appending twice the sequence 1961:2011.

Creating a column with differences based on another column

I have a data frame that looks like this (simplified from 699 treaties):
TRT <- data.frame(T.ID=c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,8),
Treaty=c("hungary slovenia 1994", "hungary slovenia 1994",
"nicaragua taiwan 2006", "nicaragua taiwan 2006",
"ukraine uzbekistan 1994", "ukraine uzbekistan 1994",
"brazil uruguay 1986", "brazil uruguay 1986",
"albania macedonia 2002", "albania macedonia 2002",
"albania moldova 2003", "albania moldova 2003",
"albania romania 2003", "albania romania 2003",
"Treaty of Izmir 1977","Treaty of Izmir 1977",
"Treaty of Izmir 1977"),
sc.y=c("HUN1994", "SLV1994", "NIC2006", "TAW2006", "UKR1994",
"UZB1994", "BRA1986", "URU1986", "ALB2002", "MAC2002",
"ALB2003", "MLD2003", "ALB2003", "RUM2003", "IRN1977",
"TUR1977", "PAK1977"),
prom.demo=c(1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0),
polity=c(10,10,8,10,7,-9,7,9,7,9,7,8,7,8,-10,-7,9))
In the end, I want to have a data frame that lists each treaty only once, its value of the “prom.demo”-column and one column that contains the difference of the maximum and minimum among the “polity”-values of the contracting parties of each treaty (most treaties have only two contracting parties, but some have up to 51).
Is there any R command that spares me 699 calculations?
Using dplyr its a join on scode and year followed by grouping by Treaty and then working out the difference between the min and max polity:
require(dplyr)
left_join(treaties, Polity, c("scode","year")) %>% group_by(Treaty) %>% summarise(PolityDiff=max(polity,na.rm=TRUE)-min(polity,na.rm=TRUE))
Source: local data frame [8 x 2]
Treaty PolityDiff
1 albania macedonia 2002 2
2 albania moldova 2003 1
3 albania romania 2003 1
4 brazil uruguay 1986 2
5 hungary slovenia 1994 0
6 nicaragua taiwan 2006 2
7 Treaty of Izmir 1977 NA
8 ukraine uzbekistan 1994 16
The NA's are where you don't have any matching scode/year (The Treaty of Izmir is IRN/TUR/PAK in 1977, and none of those are in the Polity data).
Note that if you want NA if any one of the participating countries are not in the Polity data, use:
left_join(treaties, Polity, c("scode","year")) %>% group_by(Treaty) %>% summarise(PolityDiff=max(polity)-min(polity))
which gives:
Treaty PolityDiff
1 albania macedonia 2002 2
2 albania moldova 2003 1
3 albania romania 2003 1
4 brazil uruguay 1986 2
5 hungary slovenia 1994 NA
6 nicaragua taiwan 2006 2
7 Treaty of Izmir 1977 NA
8 ukraine uzbekistan 1994 16
because Slovenia is coded as SLV in Polity but there's SLO in the treaties - mistake? Anyway, there's no SLO/1994 in Polity so that treaty returns as NA in this variant. It returns zero in my first example because the NA gets dropped and the polity difference is the difference between one number and itself...

Resources