R 中 Excel 索引匹配的等价物
Equivalent of Index matching from Excel in R
我有两个 table,Table 1 和 2
Table 如此给出
Table1 = read.table( textConnection("TimeString P3 P5 P7 P9 P11
202101152300 19.52 51.32 56.37 60.26 71.37
202101160000 19.52 51.32 56.37 60.26 71.37
202101160100 19.52 51.32 56.37 60.26 71.37
202101160200 19.52 51.32 56.37 60.26 71.37
202101160300 19.52 51.32 56.37 60.26 71.37
202101160400 19.52 51.32 56.37 60.26 71.37
202101160500 19.76 51.68 56.77 60.67 71.79
202101160600 19.76 51.68 56.77 60.67 71.79
202101160700 19.54 51.12 56.16 60.01 71.01
202101160800 19.54 51.12 56.16 60.01 71.01
202101160900 25.45 51.12 56.16 60.01 71.01
202101161000 25.45 51.12 56.16 60.01 71.01
202101161100 25.45 51.12 56.16 60.01 71.01
202101161200 25.45 51.12 56.16 60.01 71.01
202101161300 25.45 51.12 56.16 60.01 71.01
202101161400 25.45 51.12 56.16 60.01 71.01
202101161500 25.45 51.12 56.16 60.01 71.01
202101161600 25.45 54.08 59.11 75.78 105.49
202101161700 25.45 54.08 59.11 75.78 105.49
202101161800 25.45 54.08 59.11 75.78 105.49
202101161900 25.45 51.12 56.16 60.01 71.01
202101162000 25.45 51.12 56.16 60.01 71.01
202101162100 25.45 51.12 56.16 60.01 71.01
202101162200 25.73 51.68 56.77 60.67 71.79
" ), header = T)
Table 2 这是一个非常大的 table 但片段给出为
Table2 = read.table(textConnection("PNumber StartTimeString Modified
3 202101152300 TRUE
5 202101152300 TRUE
7 202101152300 TRUE
9 202101152300 TRUE
11 202101152300 TRUE
3 202101160000 TRUE
5 202101160000 TRUE
7 202101160000 TRUE
9 202101160000 TRUE
11 202101160000 TRUE
3 202101160100 TRUE
5 202101160100 TRUE
7 202101160100 TRUE
9 202101160100 TRUE
11 202101160100 TRUE
3 202101160200 TRUE
5 202101160200 TRUE
7 202101160200 TRUE
9 202101160200 TRUE
11 202101160200 TRUE
3 202101160300 TRUE
5 202101160300 TRUE
7 202101160300 TRUE
9 202101160300 TRUE
11 202101160300 TRUE
3 202101160400 TRUE
5 202101160400 TRUE
7 202101160400 TRUE
"),header = T)
现在我需要通过匹配将 Table 1 中的数字带入 Table2:两个时间字符串(Table 1 中的“TimeString”列和 Table 1 中的“StartTimeString”列Table 2) AND Table1 的列名与字母“P”和值“PNumber”的串联 Table2
中的列
我在 Excel 中使用公式解决了它,并将 table 2 转换为 Excel Table
=IF([@Modified],INDEX(Sheet1!$C:$G,MATCH([@StartTimeString],Sheet1!$B:$B,0),MATCH("P"&[@PNumber],Sheet1!$C:$G,0)),"")
结果是(这是我期待的结果)
read.table(textConnection("PTrue
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
"),header = T)
在将 Excel 代码转换为 R 时,我通过首先粘贴 PNumber 和字母“P”的值创建了新列,然后创建了一个匹配列,最后将返回值转换为数字
Table2$PNumberconcat = paste0("P",Table2$PNumber)
Table2$Match = ifelse(Table2$StartTimeString %in% Table1$TimeString,match(Table2$PNumberconcat,names(Table1)),"")
Table2$Match = as.numeric(Table2$Match)
在此之后我尝试遍历它,但它似乎不起作用 - 我得到一个空列,我错过了什么?
for(i in nrow(Table2)) {
for(j in nrow(Table1)){
Table2$PTrue[i] = ifelse(Table2$StartTimeString[i] %in% Table1$TimeString,Table1[j,Table2$Match[i]],"")
}
}
您可以使用与我不同的任何其他过程。提前致谢
你可以用 match
来做到这一点:
rowindex <- match(Table2$StartTimeString, Table1$TimeString)
columnindex <- match(paste0("P",Table2$PNumber), names(Table1))
Table2$PTrue <- Table1[cbind(rowindex, columnindex)]
它returns :
Table2
# PNumber StartTimeString Modified PTrue
#1 3 202101152300 TRUE 19.52
#2 5 202101152300 TRUE 51.32
#3 7 202101152300 TRUE 56.37
#4 9 202101152300 TRUE 60.26
#5 11 202101152300 TRUE 71.37
#6 3 202101160000 TRUE 19.52
#7 5 202101160000 TRUE 51.32
#8 7 202101160000 TRUE 56.37
#9 9 202101160000 TRUE 60.26
#10 11 202101160000 TRUE 71.37
#11 3 202101160100 TRUE 19.52
#12 5 202101160100 TRUE 51.32
#13 7 202101160100 TRUE 56.37
#14 9 202101160100 TRUE 60.26
#15 11 202101160100 TRUE 71.37
#16 3 202101160200 TRUE 19.52
#17 5 202101160200 TRUE 51.32
#18 7 202101160200 TRUE 56.37
#19 9 202101160200 TRUE 60.26
#20 11 202101160200 TRUE 71.37
#21 3 202101160300 TRUE 19.52
#22 5 202101160300 TRUE 51.32
#23 7 202101160300 TRUE 56.37
#24 9 202101160300 TRUE 60.26
#25 11 202101160300 TRUE 71.37
#26 3 202101160400 TRUE 19.52
#27 5 202101160400 TRUE 51.32
#28 7 202101160400 TRUE 56.37
我有两个 table,Table 1 和 2
Table 如此给出
Table1 = read.table( textConnection("TimeString P3 P5 P7 P9 P11
202101152300 19.52 51.32 56.37 60.26 71.37
202101160000 19.52 51.32 56.37 60.26 71.37
202101160100 19.52 51.32 56.37 60.26 71.37
202101160200 19.52 51.32 56.37 60.26 71.37
202101160300 19.52 51.32 56.37 60.26 71.37
202101160400 19.52 51.32 56.37 60.26 71.37
202101160500 19.76 51.68 56.77 60.67 71.79
202101160600 19.76 51.68 56.77 60.67 71.79
202101160700 19.54 51.12 56.16 60.01 71.01
202101160800 19.54 51.12 56.16 60.01 71.01
202101160900 25.45 51.12 56.16 60.01 71.01
202101161000 25.45 51.12 56.16 60.01 71.01
202101161100 25.45 51.12 56.16 60.01 71.01
202101161200 25.45 51.12 56.16 60.01 71.01
202101161300 25.45 51.12 56.16 60.01 71.01
202101161400 25.45 51.12 56.16 60.01 71.01
202101161500 25.45 51.12 56.16 60.01 71.01
202101161600 25.45 54.08 59.11 75.78 105.49
202101161700 25.45 54.08 59.11 75.78 105.49
202101161800 25.45 54.08 59.11 75.78 105.49
202101161900 25.45 51.12 56.16 60.01 71.01
202101162000 25.45 51.12 56.16 60.01 71.01
202101162100 25.45 51.12 56.16 60.01 71.01
202101162200 25.73 51.68 56.77 60.67 71.79
" ), header = T)
Table 2 这是一个非常大的 table 但片段给出为
Table2 = read.table(textConnection("PNumber StartTimeString Modified
3 202101152300 TRUE
5 202101152300 TRUE
7 202101152300 TRUE
9 202101152300 TRUE
11 202101152300 TRUE
3 202101160000 TRUE
5 202101160000 TRUE
7 202101160000 TRUE
9 202101160000 TRUE
11 202101160000 TRUE
3 202101160100 TRUE
5 202101160100 TRUE
7 202101160100 TRUE
9 202101160100 TRUE
11 202101160100 TRUE
3 202101160200 TRUE
5 202101160200 TRUE
7 202101160200 TRUE
9 202101160200 TRUE
11 202101160200 TRUE
3 202101160300 TRUE
5 202101160300 TRUE
7 202101160300 TRUE
9 202101160300 TRUE
11 202101160300 TRUE
3 202101160400 TRUE
5 202101160400 TRUE
7 202101160400 TRUE
"),header = T)
现在我需要通过匹配将 Table 1 中的数字带入 Table2:两个时间字符串(Table 1 中的“TimeString”列和 Table 1 中的“StartTimeString”列Table 2) AND Table1 的列名与字母“P”和值“PNumber”的串联 Table2
中的列我在 Excel 中使用公式解决了它,并将 table 2 转换为 Excel Table
=IF([@Modified],INDEX(Sheet1!$C:$G,MATCH([@StartTimeString],Sheet1!$B:$B,0),MATCH("P"&[@PNumber],Sheet1!$C:$G,0)),"")
结果是(这是我期待的结果)
read.table(textConnection("PTrue
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
60.26
71.37
19.52
51.32
56.37
"),header = T)
在将 Excel 代码转换为 R 时,我通过首先粘贴 PNumber 和字母“P”的值创建了新列,然后创建了一个匹配列,最后将返回值转换为数字
Table2$PNumberconcat = paste0("P",Table2$PNumber)
Table2$Match = ifelse(Table2$StartTimeString %in% Table1$TimeString,match(Table2$PNumberconcat,names(Table1)),"")
Table2$Match = as.numeric(Table2$Match)
在此之后我尝试遍历它,但它似乎不起作用 - 我得到一个空列,我错过了什么?
for(i in nrow(Table2)) {
for(j in nrow(Table1)){
Table2$PTrue[i] = ifelse(Table2$StartTimeString[i] %in% Table1$TimeString,Table1[j,Table2$Match[i]],"")
}
}
您可以使用与我不同的任何其他过程。提前致谢
你可以用 match
来做到这一点:
rowindex <- match(Table2$StartTimeString, Table1$TimeString)
columnindex <- match(paste0("P",Table2$PNumber), names(Table1))
Table2$PTrue <- Table1[cbind(rowindex, columnindex)]
它returns :
Table2
# PNumber StartTimeString Modified PTrue
#1 3 202101152300 TRUE 19.52
#2 5 202101152300 TRUE 51.32
#3 7 202101152300 TRUE 56.37
#4 9 202101152300 TRUE 60.26
#5 11 202101152300 TRUE 71.37
#6 3 202101160000 TRUE 19.52
#7 5 202101160000 TRUE 51.32
#8 7 202101160000 TRUE 56.37
#9 9 202101160000 TRUE 60.26
#10 11 202101160000 TRUE 71.37
#11 3 202101160100 TRUE 19.52
#12 5 202101160100 TRUE 51.32
#13 7 202101160100 TRUE 56.37
#14 9 202101160100 TRUE 60.26
#15 11 202101160100 TRUE 71.37
#16 3 202101160200 TRUE 19.52
#17 5 202101160200 TRUE 51.32
#18 7 202101160200 TRUE 56.37
#19 9 202101160200 TRUE 60.26
#20 11 202101160200 TRUE 71.37
#21 3 202101160300 TRUE 19.52
#22 5 202101160300 TRUE 51.32
#23 7 202101160300 TRUE 56.37
#24 9 202101160300 TRUE 60.26
#25 11 202101160300 TRUE 71.37
#26 3 202101160400 TRUE 19.52
#27 5 202101160400 TRUE 51.32
#28 7 202101160400 TRUE 56.37