从数据框中选择数字对
Selecting pairs of numbers from a dataframe
我有一个这样的数据框:
row col
1 1
1 50
1 55
2 75
并且我想要 select 所有配对的行和列值,以便我可以将它们插入函数中。
我正在尝试这段代码:
library(rhdf5)
for(row in df$row){
for (col in df$col){
apixel = h5read("C/path_to_a_file',, index=list(1, col, row))
apixel= data.frame(apixel)
}
}
这是按照我的意愿从我的 df 中插入我的所有 row
和 col
值,但它正在对它们进行所有可能的组合,我只想插入适当的配对。例如,row
1 应该只与 1
、50
和 55
配对作为相应的 col
,而不是 75
。
也许我可以尝试压缩两列然后 return 关联的元组?
第一步:编写函数,使其接受成对的 (row, col) 作为参数:
read_pixels <- function(row, col) {
require(rhdf5)
apixel <- h5read("C/path_to_a_file',, index=list(1, col, row))
apixel <- data.frame(apixel)
# do something with apixel
}
现在您可以将数据框的每一行传递给您的函数。有多种方法可以做到这一点,这是一个使用 apply
:
的丑陋方法
apply(myDataFrame, 1, function(x) read_pixels(x[1], x[2])
我有一个这样的数据框:
row col
1 1
1 50
1 55
2 75
并且我想要 select 所有配对的行和列值,以便我可以将它们插入函数中。
我正在尝试这段代码:
library(rhdf5)
for(row in df$row){
for (col in df$col){
apixel = h5read("C/path_to_a_file',, index=list(1, col, row))
apixel= data.frame(apixel)
}
}
这是按照我的意愿从我的 df 中插入我的所有 row
和 col
值,但它正在对它们进行所有可能的组合,我只想插入适当的配对。例如,row
1 应该只与 1
、50
和 55
配对作为相应的 col
,而不是 75
。
也许我可以尝试压缩两列然后 return 关联的元组?
第一步:编写函数,使其接受成对的 (row, col) 作为参数:
read_pixels <- function(row, col) {
require(rhdf5)
apixel <- h5read("C/path_to_a_file',, index=list(1, col, row))
apixel <- data.frame(apixel)
# do something with apixel
}
现在您可以将数据框的每一行传递给您的函数。有多种方法可以做到这一点,这是一个使用 apply
:
apply(myDataFrame, 1, function(x) read_pixels(x[1], x[2])