I'm searching for the maximum value within a certain range of rows in a csv file. I figured out the range, but now that I want to determine the maximum value it just reveals the position of that particular value instead of the actual value.
我正在搜索csv文件中特定行范围内的最大值。我想出了范围,但现在我想确定最大值,它只是显示特定值的位置而不是实际值。
initial situation as follows:
初步情况如下:
i read the file
我读了这个文件
A <- read.csv(file=data[1], header=TRUE, sep=";")
next I determine the range investigating
接下来我确定调查的范围
Duration = as.character (A [16,1])
Duration = as.numeric(strsplit(Duration," ") [[1]][2])
B<- A[which(A==28447)]
so these are the target rows:
所以这些是目标行:
[943] 0.0
[944] 1.8771
[945] 1.2928
[946] 1.7946
[947] 2.3201
[948] 1.8459
[949] 1.5889
[950] 1.3549
[951] 1.2376
[952] 1.1296
[953] 1.0619
[954] 1.0132
[955] 0.9684
[956] 0.93
[957] 0.8976
now what I did was:
现在我做的是:
A[(B+1):(B+Duration),1]
revealing that:
[1] 0.0 1.8771 1.2928 1.7946 2.3201 1.8459 1.5889 1.3549 1.2376 1.1296 1.0619 1.0132 0.9684 0.93 0.8976
24582 Levels: -0.0 -0.001 -0.0011 -0.0012 -0.0014 -0.0016 -0.0017 -0.0018 -0.0019 -0.0022 -0.0024 -0.0026 -0.0028 -0.003 -0.0033 ... Timesteps: 15
If I now go for the max((B+1):(B+Duration),1)
I get:
如果我现在去最大((B + 1):( B +持续时间),1)我得到:
957
How to I avoid that?
我该怎么做?
highly appreciate your help.
非常感谢你的帮助。
cheers Olli
2 个解决方案
#1
#2
1
which.max
returns the position of the maximum value in the vector:
which.max返回向量中最大值的位置:
> A <- rnorm(10,0,1)
> A
[1] -1.05831869 -0.20686273 -0.81552863 -0.47213498 0.87199615 0.06288441 0.94777881 0.46776767 1.91153029
[10] -0.59206801
>
> which.max(A)
[1] 9
So, to illustrate this, you could use this to index to the maximum value:
因此,为了说明这一点,您可以使用它来索引最大值:
> A[which.max(A)]
[1] 1.91153
Much simpler though is the max
function!
更简单的是最大功能!
> max(A)
[1] 1.91153
>
If you're getting a not meaningful for factors error you need to convert to numeric first. How to convert a factor to integer\numeric without loss of information?
如果您对因子错误没有意义,则需要先转换为数字。如何在不丢失信息的情况下将因子转换为整数\数字?
#1
3
which.max
appropriately1 returns the index. You just want max
instead.
which.max proper1返回索引。你只想要max。
1which
in R function names usually refers to indices. For example, which(A[, 1] > 2)
will return the indices of all elements greater than 2.
1在R函数名称中通常是指索引。例如,哪个(A [,1]> 2)将返回大于2的所有元素的索引。
#2
1
which.max
returns the position of the maximum value in the vector:
which.max返回向量中最大值的位置:
> A <- rnorm(10,0,1)
> A
[1] -1.05831869 -0.20686273 -0.81552863 -0.47213498 0.87199615 0.06288441 0.94777881 0.46776767 1.91153029
[10] -0.59206801
>
> which.max(A)
[1] 9
So, to illustrate this, you could use this to index to the maximum value:
因此,为了说明这一点,您可以使用它来索引最大值:
> A[which.max(A)]
[1] 1.91153
Much simpler though is the max
function!
更简单的是最大功能!
> max(A)
[1] 1.91153
>
If you're getting a not meaningful for factors error you need to convert to numeric first. How to convert a factor to integer\numeric without loss of information?
如果您对因子错误没有意义,则需要先转换为数字。如何在不丢失信息的情况下将因子转换为整数\数字?