问题
I have a dataframe with a value column for multiple year. The years might not follow a sequence and might have a missing 5th year. Here is an example dataframe
df = data.frame(code = c("AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS", "AUT", "AUT", "AUT", "AUT", "ABW", "AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ARM"),
PPT = c(123, 42, 23, 5, 42, 4, 23, 25, 42, 23, NA, 5563, 56, 54, 645, 6, 4,53, 656, 65, 5563, 646, 6, 66, 54),
Year = c(1990, 1991, 1992, 1993, 1991, 1995, 1996, 1997, 1991, 1992, 2000, 2001, 2002, 2014, 2004, 2005, 2006, 2007, 1960, 2009, NA, 2011, 2012, 2013, 2014))
I want to add an additional column that will be based on the difference between the value for that year and the year+5. Ex. If the first year in the year column is 1960 but no PPT data is available for 1965, therefore the value in the new_col would be NA. Similarly, the value for new_col for the year 1990 would be 119(123-4), NA for the year 2000(no PPT data available for 2005 ), 19 for 1991 and -2 for the year 1992 and so on.
I have a very convoluted way of doing this in excel, however, I am looking for an easier solution in R
回答1:
We can arrange
by 'Year', and take the difference of 'PPT' with lead
of 'PPT' where the 'n' is specified as 5
library(dplyr)
df %>%
arrange(Year) %>%
mutate(newcol = PPT - lead(PPT, n = 5, default = 0))
# code PPT Year newcol
#1 AFG 123 1990 119
#2 AGO 42 1991 19
#3 ALB 23 1992 -2
#4 AND 5 1993 -1
#5 ARB 23 1994 -611
#6 ARE 4 1995 -1
#7 ARG 23 1996 -5540
#8 ARM 25 1997 -31
#9 ASM 6 1998 -50
#10 ATG 634 1999 -11
#...
if some 'Year's are missing, we can expand the data with complete
and then do the mutate
library(tidyr)
df %>%
arrange(Year) %>%
complete(Year = min(Year):max(Year)) %>%
mutate(newcol = PPT - lead(PPT, n = 5, default = 0)) %>%
filter(!is.na(PPT))
Or using base R
df$newcol <- with(df, c(head(PPT, -5) - tail(PPT, -5), tail(PPT, 5)))
data
df <- structure(list(code = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 13L, 13L, 13L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 9L), .Label = c("ABW", "AFG", "AGO", "ALB", "AND",
"ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS", "AUT"), class = "factor"),
PPT = c(123, 42, 23, 5, 23, 4, 23, 25, 6, 634, 5, 5563, 56,
56, 645, 6, 4, 656, 645, 65, 5563, 646, 6, 66, 54),
Year = 1990:2014), class = "data.frame", row.names = c(NA,
-25L))
回答2:
A data.table solution that will work with missing/gapped years...
sample data
df = data.frame(code = c("AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ASM", "ATG", "AUS", "AUT", "AUT", "AUT", "AUT", "ABW", "AFG", "AGO", "ALB", "AND", "ARB", "ARE", "ARG", "ARM", "ARM"),
PPT = c(123, 42, 23, 5, 23, 4, 23, 25, 6, 634, 5, 5563, 56, 56, 645, 6, 4, 656, 645, 65, 5563, 646, 6, 66, 54),
Year = c(1990:2014))
code
library(data.table)
#create a data.table with all years from minimum untill maximum + 5
#so missing years will get a NA!
#perform a by-reference join on these years, by Year
result <- data.table( Year = min(df$Year):(max(df$Year) + 5) )[setDT(df), `:=`(code = i.code, PPT = i.PPT), on = .(Year)]
#calculate the desired column, delete unwanted rows
result[, newcol := PPT - shift(PPT, 5, type = "lead" )][!is.na(code),][]
output
# Year code PPT newcol
# 1: 1990 AFG 123 119
# 2: 1991 AGO 42 19
# 3: 1992 ALB 23 -2
# 4: 1993 AND 5 -1
# 5: 1994 ARB 23 -611
# 6: 1995 ARE 4 -1
# 7: 1996 ARG 23 -5540
# 8: 1997 ARM 25 -31
# 9: 1998 ASM 6 -50
# 10: 1999 ATG 634 -11
# 11: 2000 AUS 5 -1
# 12: 2001 AUT 5563 5559
# 13: 2002 AUT 56 -600
# 14: 2003 AUT 56 -589
# 15: 2004 AUT 645 580
# 16: 2005 ABW 6 -5557
# 17: 2006 AFG 4 -642
# 18: 2007 AGO 656 650
# 19: 2008 ALB 645 579
# 20: 2009 AND 65 11
# 21: 2010 ARB 5563 NA
# 22: 2011 ARE 646 NA
# 23: 2012 ARG 6 NA
# 24: 2013 ARM 66 NA
# 25: 2014 ARM 54 NA
# Year code PPT newcol
回答3:
We can also use mapply
df$new_col <- mapply(function(x, y) {
inds = df$Year == y + 5
if (any(inds)) x - df$PPT[inds] else x
},df$PPT, df$Year)
df
# code PPT Year new_col
#1 AFG 123 1990 119
#2 AGO 42 1991 19
#3 ALB 23 1992 -2
#4 AND 5 1993 -1
#5 ARB 23 1994 -611
#6 ARE 4 1995 -1
#7 ARG 23 1996 -5540
#8 ARM 25 1997 -31
#9 ASM 6 1998 -50
#10 ATG 634 1999 -11
#.....
来源:https://stackoverflow.com/questions/55944339/arithmetic-operation-based-on-value-from-another-column