我的问题很简单,但我是一个新的R用户。。。
我有一个带参数的函数。我想把结果放在一个向量中,每个函数调用都有一个特定的名称。
我的职能
`Window_length<-function(x,y) {
first_interval<-length(which(x <= -1.5))
second_interval<-length(which(x <= -1 & x > -1.5 ))
third_interval<-length(which(x <= -0.5 & x > -1 ))
fourth_interval<-length(which(x <= 0 & x > -0.5 ))
fifth_interval<-length(which(x <= 0.5 & x > 0 ))
sixth_interval<-length(which(x <= 1 & x > 0.5 ))
seventh_interval<-length(which(x <= 1.5 & x > 1 ))
eighth_interval<-length(which(x <= 2 & x > 1.5 ))
ninth_interval<-length(which(x > 2 ))
y <<- c(
rep("1",first_interval),
rep("2",second_interval),
rep("3",third_interval),
rep("4",fourth_interval),
rep("5",fifth_interval),
rep("6",sixth_interval),
rep("7",seventh_interval),
rep("8",eighth_interval),
rep("9",ninth_interval))}`
因此,当我调用Window\u length时,我想将结果放入给定的变量中,例如:
`Window_length(data,output_result)`
在输出结果中,我希望得到“y”值。
而且我确信我的代码一点也不完美。如果有人能帮我优化一下我的代码,那就太好了。
我之所以要做这些,是因为我需要用ggplot的数据做一个绘图。我的值介于-4和+3之间。我想创建一个带有特定窗口的绘图(<-1.5/-1.5:-1/-1:-0.5/-0.5:0/0:1/1:1.5/1.5:2/>2)
My data :
data<- c(-3.7865964 -3.7865964 -3.1975372 -3.1975372 -3.169925 -3.1292830 -3.1292830 -2.6629650 -2.4739312 -2.4739312 -2.3536370 -2.3536370 -2.2446224 -2.2446224 -2.0000000 -1.8744691 -1.8744691 -1.7705182 -1.7655347 -1.7655347 -1.7472339 -1.7472339 -1.7062688 -1.7036070........... 1.8744691 1.8744691 2.0000000 2.2446224 2.2446224 2.3536370)
length(data)=21685
To_Be_Plot = data.frame(data,y)
fig1<-ggplot(To_Be_Plot, aes(x=y, y=data))+geom_boxplot()
预期结果:
谢谢大家