首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >While循环在这里是否实际合适,还是我(再次)误解了应用程序的函数族?

While循环在这里是否实际合适,还是我(再次)误解了应用程序的函数族?
EN

Stack Overflow用户
提问于 2014-10-17 14:29:55
回答 1查看 89关注 0票数 0

我是R的一个新用户,我更习惯于使用Matalab等程序的循环函数,但鉴于R的优点和缺点,我试图避开循环,而更倾向于应用函数。唯一的问题是,我不能百分之百地确定什么时候应该优先于另一个,例如在下面的场景中:

我有一系列的数据,我想转换成个别的情节。我希望这些图通过定位一个特定的点,然后确定该图的xlim和ylim,从而使图形只包含80个或更少的点,从而放大到绘图的某个区域。这可能是也可能不是更糟糕的方式,但它已经奏效了。

对于代码(德语中的Weg =距离,卡夫=应变,Zeit = time):

代码语言:javascript
复制
#reads the data
mydata <- read.table(file, header = TRUE, skip=52, dec=",")

#establishes what segments of mydata are what

kraft  <- mydata[,2]
weg    <- mydata[,3]
zeit   <- mydata[,1]

#Finds the distance values associated with the maximum force in the plot, which is the area in which I am interested

Weg_Values_at_Fmax <- weg[which(kraft == max(kraft))]


#the next sets of lines initiate the values which will be changed in the while loop
#one to zero, the other to a range of distance values on either side of the distance #values associated with the maximum force
n <- 0

Weg.index <- which((weg >= Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)] - 
                    Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)]*(1/7)  + n) & 
                    weg <= (Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)] + 
                    Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)]*(1/7) - n))      

#finally the while loop which increase n (thereby decreasing Weg.index) until the #Weg.index falls below a certain value, in this case 80 points #                                                                                                                                                                     
while(length(Weg.index) > 80){

    n <- n + .0005
    Weg.index <- which((weg >= Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)] - 
                        Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)]*(1/6)  + n) & 
                        weg <= (Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)] +
                        Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)]*(1/6) - n))

}

接下来是密谋:

代码语言:javascript
复制
plot(weg, kraft, 
     xlim=c(weg[Weg.index[1]], weg[Weg.index[length(Weg.index)]]), 
     ylim=c(min(kraft[Weg.index[1:length(Weg.index)]]),
            max(kraft[Weg.index[1:length(Weg.index)]])),
     main = file)

这段代码工作正常,因为没有太多的数据,但是我希望有一种更有效的方法来处理这样的数据请求,当我需要处理更大的数据时,可以使用while循环。我希望这个问题足够具体,不要太离题或诸如此类的问题。谢谢你的时间和帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-21 19:50:47

我采用了将数据保存在一个数据框架中的方法,并使用percentile()函数及其逆ecdf()来发现所需的值范围。下面的代码不应该依赖于数据的任何顺序。如果有相同的“聚光灯”卡夫值的多个记录,它也应该表现得很好。注意,聚光灯是匹配值的第一个。

代码语言:javascript
复制
plot.points=80

#call spotlight the index of row to highlight
spotlight.kraft <-  max(mydata$Kraft_N)
spotlight <- which(mydata$Kraft_N == spotlight.kraft)[1]

#get the quantiles of the values above/below spotlight.kraft
x.p = c(ecdf(mydata$Weg_mm)(mydata$Weg_mm[spotlight]) - 0.5 *plot.points/nrow(mydata),
        ecdf(mydata$Weg_mm)(mydata$Weg_mm[spotlight]) + 0.5 *plot.points/nrow(mydata))

#adjust for quantiles outside [0,1]
if(x.p[1]<0) x.p <- x.p -x.p[1]
if(x.p[2]>1) x.p <- x.p - x.p[2] + 1

xlims <- quantile(mydata$Weg_mm,x.p)

#possibly not exactly 80 points shown
nrow(subset(mydata, Weg_mm > xlims[1] & Weg_mm<xlims[2]))
#get corresponding kraft values
ylims <- range(subset(mydata, Weg_mm > xlims[1] & Weg_mm<xlims[2])$Kraft_N)

plot(weg, kraft,  xlim=xlims, ylim=ylims)

不需要用循环来完成它。

顺便说一句,您的原始代码相当多地使用了这个结构。

代码语言:javascript
复制
Weg_Values_at_Fmax[length(Weg_Values_at_Fmax)]

根据R中max()函数的定义,它始终是长度为1的向量。因此,这个表达式总是只返回Weg_Values_at_Fmax的标量值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26427268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档