如何读取多个变量的最大值,并使用R中的空间接近值形成平均值?
啊,, 下面的df与我必须使用的df相似,但要小得多: (我省去了很多行,以便于观看。) 上面的df是由荧光板读取器创建的,该读取器将一个区域内的光强度划分为25个扇区(5x5),并分别测量每个扇区,每个扇区给出一个值,从而扫描该区域内的光强度。测量顺序为左上角扇区第一,右下角扇区最后。要使其更具图形化,请执行以下操作:如何读取多个变量的最大值,并使用R中的空间接近值形成平均值?,r,sorting,R,Sorting,啊,, 下面的df与我必须使用的df相似,但要小得多: (我省去了很多行,以便于观看。) 上面的df是由荧光板读取器创建的,该读取器将一个区域内的光强度划分为25个扇区(5x5),并分别测量每个扇区,每个扇区给出一个值,从而扫描该区域内的光强度。测量顺序为左上角扇区第一,右下角扇区最后。要使其更具图形化,请执行以下操作: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 填入上面给出的d
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
填入上面给出的df值(+坐标),它将如下所示:
(5) 9-2-9-8-4
(4) 7-7-2-5-3
(3) _uuuu5-4-7-8-9
(2) 6-6-3-5-9
(1) 4-7-9-7-10
(一)(二)(三)(四)(五)
我需要的是读取每个变量的最大值,并计算该值的平均值和(最多)9个围绕它的字段。在上面的区域/“变量”(“a”)中,扇区的最高值在右下角为10,该值由值5、9和7包围。因此,我寻找的变量“a”的结果是7.75((5+9+7+10)/4)
我想象代码类似于这样(我知道这不是您编写r的方式,但我不太清楚):
平均值(变量内的最大值,x处的值(变量内的最大值)-1,y处的值(变量内的最大值)),x处的值(变量内的最大值)-1,y处的值(变量内的最大值)+1
下一个问题的挑战是,仪器将执行96个区域(“变量”)的扫描。理想情况下,我需要一个解决方案,自动为每个/所有变量提供这个特殊的平均值,而不需要我写几乎相同的代码96次
我知道这要求有点高,但我已经做了一段时间了,我就是想不出一个解决方案,甚至是谷歌搜索的好方法
非常感谢您的帮助
蒂姆
Ps:使用此R代码创建上面显示的df的随机版本:
df <- data.frame(x = c(1:5), y = rep(c(5:1), each=5),variable = rep(c("a", "b"), each=25 ), values = floor(runif(50, min=1, max=10)))
df此更新答案将提供最大值的平均值,每个变量组内最多有9个周围值
library(dplyr)
# Create the function
get.means <- function(df){
# Get a data frame of rows with the maximum value
max.rows <- df[df$values == max(df$values), ]
# Create an empty data frame
means.df <- data.frame(variable = character(), x = integer(), y = integer(), value = numeric(), mean = numeric(), stringsAsFactors = FALSE)
# Create an iterator for the data frame
iterator <- 1
# Loop through each row of the maximum value data frame
for(i in c(1:nrow(max.rows))){
# Get the x value for the current row
x <- max.rows$x[i]
# Get the y value for the current row
y <- max.rows$y[i]
# Set the range of x values to process based on the x coordinate
if(x == 1){
x.range <- c(1, 2)
} else if(x == 5){
x.range <- c(4, 5)
} else{
x.range <- c(x-1, x, x+1)
}
# Set the range of y values to process based on the y coordinate
if(y == 1){
y.range <- c(1, 2)
} else if(y == 5){
y.range <- c(4, 5)
} else{
y.range <- c(y-1, y, y+1)
}
# Get a matrix of the values from the original data frame, which are in both the current y and x ranges
vals <- as.matrix(df[(df$y %in% y.range) & (df$x %in% x.range), 'values'])
# Get the mean of the values
mean.val <- mean(vals)
# Insert the current variable value to the data frame for the new row
means.df[iterator, 'variable'] <- as.character(max.rows$variable[i])
# Insert the current x, y, value, and mean values for the new row
means.df[iterator, c('x','y','value', 'mean')] <- c(x, y, max.rows$values[i], mean.val)
# Increment the iterator
iterator <- iterator + 1
}
# Return the final data frame
return(means.df)
}
# Create a test data frame
df <- data.frame(x = c(1:5), y = rep(c(5:1), each=5),variable = rep(c("a", "b"), each=25 ), values = floor(runif(50, min=1, max=10)))
# Get the means for each max value within the variable grouping
df1 <- df %>%
group_by(variable) %>%
do(get.means(.))
库(dplyr)
#创建函数
get.means您可以使用SO编辑工具来提高代码和文本的可读性吗?我很难理解你想做什么。谢谢。我很想去,但很明显,我也无法做到这一点。不过,我确实接受了你的改进建议。非常感谢你的建议。我确实认为它朝着正确的方向发展。然而,这仍然给我留下了一个问题,我最终只希望每个变量有一个值,并将它们全部放在一个df中。多亏了你,我才意识到我的榜样相当糟糕。不仅存在多个最大值,而且我展示的示例计算甚至没有使用该特定扇区的最大值。我调整了我的例子,改变了措辞。我希望现在更清楚了。也许你可以再看一眼。谢谢TimI已更新我的答案,以便使用dplyrgroup\u by
功能。现在,它将返回一个数据帧,每个变量中最大值的平均值最多为9个周围值。注意:如果变量组中的最大值有对应关系,那么它将返回两行。Wohooo。非常感谢你!据我所知,这是现场。我唯一能提供的回应是,我将努力在编码和格式方面做得更好,这样我就可以在将来为社区做出贡献!非常感谢。
library(dplyr)
# Create the function
get.means <- function(df){
# Get a data frame of rows with the maximum value
max.rows <- df[df$values == max(df$values), ]
# Create an empty data frame
means.df <- data.frame(variable = character(), x = integer(), y = integer(), value = numeric(), mean = numeric(), stringsAsFactors = FALSE)
# Create an iterator for the data frame
iterator <- 1
# Loop through each row of the maximum value data frame
for(i in c(1:nrow(max.rows))){
# Get the x value for the current row
x <- max.rows$x[i]
# Get the y value for the current row
y <- max.rows$y[i]
# Set the range of x values to process based on the x coordinate
if(x == 1){
x.range <- c(1, 2)
} else if(x == 5){
x.range <- c(4, 5)
} else{
x.range <- c(x-1, x, x+1)
}
# Set the range of y values to process based on the y coordinate
if(y == 1){
y.range <- c(1, 2)
} else if(y == 5){
y.range <- c(4, 5)
} else{
y.range <- c(y-1, y, y+1)
}
# Get a matrix of the values from the original data frame, which are in both the current y and x ranges
vals <- as.matrix(df[(df$y %in% y.range) & (df$x %in% x.range), 'values'])
# Get the mean of the values
mean.val <- mean(vals)
# Insert the current variable value to the data frame for the new row
means.df[iterator, 'variable'] <- as.character(max.rows$variable[i])
# Insert the current x, y, value, and mean values for the new row
means.df[iterator, c('x','y','value', 'mean')] <- c(x, y, max.rows$values[i], mean.val)
# Increment the iterator
iterator <- iterator + 1
}
# Return the final data frame
return(means.df)
}
# Create a test data frame
df <- data.frame(x = c(1:5), y = rep(c(5:1), each=5),variable = rep(c("a", "b"), each=25 ), values = floor(runif(50, min=1, max=10)))
# Get the means for each max value within the variable grouping
df1 <- df %>%
group_by(variable) %>%
do(get.means(.))