r - 尝试根据 R df 中的给定对在 Excel 单元格中查找值

标签 r excel dataframe matrix

我正在使用当前已读入 R 的 Excel 工作表:https://www.knomad.org/sites/default/files/2018-04/bilateralmigrationmatrix20170_Apr2018.xlsx

dput(head(remittance, 5))

输出为:

structure(list(`Remittance-receiving country (across)                                                              -                                                 Remittance-sending country (down)` = c("Australia", 
"Brazil", "Canada"), Brazil = c("27.868809286999106", "0", "31.284184411144214"
), Canada = c("46.827693406219382", "1.5806325278762619", "0"
), `Czech Republic` = c("104.79905129342241", "3.0488843262423089", 
"176.79676736179096"), Finland = c("26.823089572300752", "1.3451674211686246", 
"37.781150857376964"), France = c("424.37048861305249", "123.9763417712491", 
"1296.7352242506483"), Germany = c("556.4140279523856", "66.518143815367239", 
"809.9621650533453"), Hungary = c("200.08597014449356", "11.953328254521287", 
"436.0811601171776"), Indonesia = c("172.0021287331823", "1.3701340430259537", 
"33.545925908780198"), Italy = c("733.51652291459231", "116.74264895322995", 
"1072.1119887588022"), `Korea, Rep.` = c("259.97044386689589", 
"20.467939414361016", "326.94157937864327"), Netherlands = c("133.48932759488602", 
"4.7378343766684532", "181.28828076733771"), Philippines = c("1002.3593555086774", 
"1.5863355979877207", "2369.5223195675494"), Poland = c("109.73486651698796", 
"5.8313637459523129", "341.10408952685464"), `Russian Federation` = c("19.082541158574934", 
"1.0136604494838692", "58.760989426089431"), `Saudi Arabia` = c("13.578431465294949", 
"0.32506772760873404", "15.511213677040857"), Sweden = c("91.887827513176489", 
"5.1132733094740352", "65.860232580192786"), Thailand = c("383.08245004577498", 
"2.7410805494977684", "79.370683058792849"), `United Kingdom` = c("1084.0742194994727", 
"4.2050614573174592", "568.62605950140266"), `United States` = c("188.06242727403128", 
"49.814372612310521", "661.98049661387927"), WORLD = c("5578.0296723604206", 
"422.37127035334271", "8563.264510816849")), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

我目前有一个由两列“源”和“目的地”组成的数据框,其中每行是我通过执行以下操作创建的一对国家/地区:

countries = c("Australia","Brazil", "Canada", "Czech Republic", "Germany", "Finland", "United Kingdom", "Italy", "Poland", "Russian Federation", "Sweden", "United States", "Philippines", "France", "Netherlands", "Hungary", "Saudi Arabia", "Thailand", "Korea, Rep.", "Indonesia")


pairs = t(combn(countries, 2))

我想使用每一对从上面的 Excel 工作表中提取其相应的值。 (在 Excel 工作表中,“来源”是国家/地区向下的第一列,“目的地是国家/地区之间的第一行”

例如,我拥有的 df 示例如下(当前包含 190 对):

pairs = data.frame(Source = c("Australia", "Australia", "Australia"), Destination = c("Brazil", "Canada", "Czech Republic"))

我的 df 中的第一对是(澳大利亚、巴西),对应于我上面复制的 Excel 工作表中的值 27.868809286999106。是否有一个内置的 R 函数可以匹配我的 df 中的对以提取其相应的值?谢谢

最佳答案

也许您需要的是dplyr::pivot_longer

library(dplyr)

colnames(remittance)[1] <- 'source'

remittance %>% pivot_longer(-source, names_to = 'destination')


#----
# A tibble: 60 x 3
   source    destination    value             
   <chr>     <chr>          <chr>             
 1 Australia Brazil         27.868809286999106
 2 Australia Canada         46.827693406219382
 3 Australia Czech Republic 104.79905129342241
 4 Australia Finland        26.823089572300752

注意remittance是OP dput中的数据帧。

关于r - 尝试根据 R df 中的给定对在 Excel 单元格中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65959088/

相关文章:

r - 如何计算列中每个类别的份额?

excel - 根据范围内的最高值设置列颜色

Excel 求解器和 VBA : Floating point/decimal numbers in constraints get incorrectly converted to integers?

r - 选择相关矩阵的变量

r中散点图的回归线和拟合曲线

excel - AppleScript Tell 应用程序 Microsoft Excel 使用 Parallels 在 Mac 上打开 Windows Excel?

python - 基于另一列复制行

r - 循环具有半类似列名的列

python - 如果与最后一行连续出现,则添加值,否则保持相同的值

r - 使用R在 map 中绘制电子邮件流