My question is pretty simple but I'm a new R user...
So I have a function which took an argument. I want to put the results in a vector with a specific name for each call to the function.
My function
`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))}`
So when I call Window_length, I want to put the results into a given variable for example :
`Window_length(data,output_result)`
In output_result I expect to have the "y" values.
Also I'm sure that my code is not perfect at all. If someone can help me to optimized a little bit my code it's will be nice.
I'm trying to make all of this because I need to make a plot with ggplot of data. My value are between -4 and +3. And I want to create a plot with specific window ( <-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()
One solution, if I understood correctly the issue, would be to use the function cut
:
x <- seq(-2.9, 3, l=5000)
FC <- sin(x*pi) + x^2/10 + 0.1*rnorm(5000)
dat <- data.frame(x, FC)
dat$windows <- cut(dat$x, breaks = seq(-3, 3, by=1))
ggplot(data=dat, aes(x, FC, color=windows)) +
geom_boxplot() + theme_bw()
The resulting command plot boxplots to display the windows.