Если я правильно понял, вы можете использовать функцию 'collect ()' tidyr
и использовать within()
функцию base
.
Проверьте, решает ли она вашу проблему:
library(dplyr)
library(tidyr)
# Yours data
to_add = data.frame(a=c(NA,NA),
b=c(NA,NA),
c=c(89.08,60.2),
d=c(NA,NA)
)
colnames(to_add) = c("1999","2000","2001","2002")
# Creating column of "coin"
to_add["coin"] = c("EUR","USD")
# Just numbers of rows and columns
nr = nrow(to_add)
nc = ncol(to_add)-1
# Transforming data, like "transpose"
to_add = gather(data = to_add,"year","value",1:4)
# build another data frame
euro_adj = data.frame(flow=rep("Exports", nc*nr),
country=rep("Austria", nc*nr),
year=rep(1999:2002, nr),
frequency=rep("Annual", nc*nr),
# Set curency by order
currency= to_add[order(to_add$coin),"coin"],
percentage=NA[1:nc*nr],
notes=NA[1:nc*nr]
)
# set values
set = to_add[!is.na(to_add$value),c("coin","year","value")]
euro_adj = euro_adj %>%
within(percentage[year %in% set$year & currency %in% set$coin] <- set$value)
# Print data frame
euro_adj
Который дает:
flow country year frequency currency percentage notes
Exports Austria 1999 Annual EUR NA NA
Exports Austria 2000 Annual EUR NA NA
Exports Austria 2001 Annual EUR 89.08 NA
Exports Austria 2002 Annual EUR NA NA
Exports Austria 1999 Annual USD NA NA
Exports Austria 2000 Annual USD NA NA
Exports Austria 2001 Annual USD 60.20 NA
Exports Austria 2002 Annual USD NA NA