Reynaldo Senra
Reynaldo Senra

Reputation: 31

How to change all the unique values of a variable in dataframe R

I will appreciate any help with this. I think it should be simple, but I haven't found any solution in Internet. I have the following sample dataframe

ddd <- structure(list(code = structure(c(174L, 174L, 174L, 174L, 174L, 
174L, 204L, 204L, 204L, 204L, 204L, 204L), .Label = c("ABW", 
"AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", 
"ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD", 
"BGR", "BHR", "BHS", "BIH", "BLR", "BLZ", "BMU", "BOL", "BRA", 
"BRB", "BRN", "BTN", "BWA", "CAF", "CAN", "CEB", "CHE", "CHI", 
"CHL", "CHN", "CIV", "CMR", "COD", "COG", "COL", "COM", "CPV", 
"CRI", "CSS", "CUB", "CUW", "CYM", "CYP", "CZE", "DEU", "DJI", 
"DMA", "DNK", "DOM", "DZA", "EAP", "EAR", "EAS", "ECA", "ECS", 
"ECU", "EGY", "EMU", "ERI", "ESP", "EST", "ETH", "EUU", "FCS", 
"FIN", "FJI", "FRA", "FRO", "FSM", "GAB", "GBR", "GEO", "GHA", 
"GIB", "GIN", "GMB", "GNB", "GNQ", "GRC", "GRD", "GRL", "GTM", 
"GUM", "GUY", "HIC", "HKG", "HND", "HPC", "HRV", "HTI", "HUN", 
"IBD", "IBT", "IDA", "IDB", "IDN", "IDX", "IMN", "IND", "INX", 
"IRL", "IRN", "IRQ", "ISL", "ISR", "ITA", "JAM", "JOR", "JPN", 
"KAZ", "KEN", "KGZ", "KHM", "KIR", "KNA", "KOR", "KWT", "LAC", 
"LAO", "LBN", "LBR", "LBY", "LCA", "LCN", "LDC", "LIC", "LIE", 
"LKA", "LMC", "LMY", "LSO", "LTE", "LTU", "LUX", "LVA", "MAC", 
"MAF", "MAR", "MCO", "MDA", "MDG", "MDV", "MEA", "MEX", "MHL", 
"MIC", "MKD", "MLI", "MLT", "MMR", "MNA", "MNE", "MNG", "MNP", 
"MOZ", "MRT", "MUS", "MWI", "MYS", "NAC", "NAM", "NCL", "NER", 
"NGA", "NIC", "NLD", "NOR", "NPL", "NRU", "NZL", "OED", "OMN", 
"OSS", "PAK", "PAN", "PER", "PHL", "PLW", "PNG", "POL", "PRE", 
"PRI", "PRK", "PRT", "PRY", "PSE", "PSS", "PST", "PYF", "QAT", 
"ROU", "RUS", "RWA", "SAS", "SAU", "SDN", "SEN", "SGP", "SLB", 
"SLE", "SLV", "SMR", "SOM", "SRB", "SSA", "SSD", "SSF", "SST", 
"STP", "SUR", "SVK", "SVN", "SWE", "SWZ", "SXM", "SYC", "SYR", 
"TCA", "TCD", "TEA", "TEC", "TGO", "THA", "TJK", "TKM", "TLA", 
"TLS", "TMN", "TON", "TSA", "TSS", "TTO", "TUN", "TUR", "TUV", 
"TZA", "UGA", "UKR", "UMC", "URY", "USA", "UZB", "VCT", "VEN", 
"VGB", "VIR", "VNM", "VUT", "WLD", "WSM", "XKX", "YEM", "ZAF", 
"ZMB", "ZWE"), class = "factor"), year = c("1960", "1961", "1962", 
"1963", "1964", "1965", "1960", "1961", "1962", "1963", "1964", 
"1965"), gdp = c(0.148355493351994, 0.167249825052484, 0.172848145556333, 
0.179146256123163, 0.175647305808257, 0.186144156752974, 0.0832750174947515, 
0.0944716585024493, 0.100069979006298, 0.0965710286913926, 0.115465360391882, 
0.118964310706788)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

I want to replace all unique values in the variable year (from 1960 to 1965) by other values, lets say by 1961 to 1966. In other words, I want to replace all 1960s in the current dataframe by 1961s, all 1961s in the current dataframe by 1962s and so on. Of course, this is a sample and here this can be done one by one (I found how to do this in Internet). However, the dataframe I have has 200 unique values for the variable "code" and 70 years for the variable "year". So, it is unmanageable to do the change one by one.

Very much thank you in advance

Upvotes: 0

Views: 268

Answers (1)

Ric
Ric

Reputation: 5722

ddd$year <- as.character(as.numeric(ddd$year)+1)

Upvotes: 1

Related Questions