[R] need for help for solving operations in a vector

David L Carlson dcarlson at tamu.edu
Wed Dec 23 20:57:15 CET 2015


I don't think you know what your code is doing. First, do not use html emails, only plain text. Secondly, provide the data in a portable way with the dput() function:

> dput(qWC1)
c(75.33336, 75.20617, 75.20617, 74.95275, 74.95275, 74.70059, 
74.70059, 74.70059, 74.57498, 74.44968, 74.32469, 74.07563, 85.57237, 
90.40123, 90.7376, 90.7376, 90.7376, 90.7376, 90.90648, 91.07582, 
91.24564, 90.90648, 86.82135, 80.69793, 79.30393, 78.62058, 78.21484, 
77.81226, 77.67876, 77.41279, 77.28032, 76.88495, 76.75383, 76.75383, 
76.4926, 76.36249, 76.2327, 76.2327, 76.10325, 75.97412, 75.84532, 
75.71685, 75.71685, 75.71685, 75.71685, 75.46087, 75.46087, 75.46087, 
75.33336, 75.20617, 75.20617, 75.20617, 75.20617, 75.20617, 75.20617, 
75.0793, 75.0793, 75.0793, 74.95275, 74.95275, 74.95275)

Third, your code is a mess. You do not seem to understand how R uses "<-", "==", and "=" in different ways so your y value has nothing to do with what you say you want. 

First, your loop will create missing values because you have 61 values, but you refer to i+1 (62) and i+2 (63) which do not exist. 

Second, if x[i+1] > x[i] is true, you execute y[i] == x[i+1] - x[i] which tests to see if the left side is equal to the right side and returns TRUE or FALSE, but does not change y[i] at all. The same with the last else statement. 

Once you figure out what you are doing, you can probably handle it all with ifelse() and not use a for loop at all. Or try combining the results of diff(x, 1) and diff(x, 2). You should not try to program in R until you have read more about the R language.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Makram Belhaj Fraj
Sent: Wednesday, December 23, 2015 1:35 AM
To: r-help at r-project.org; r-help-owner at r-project.org
Subject: [R] need for help for solving operations in a vector

 Dear colleagues
i need your generous help to solve the following problem

I have a  soil moisture time series qWC1 (61 values)
> qWC1
 75.33336 75.20617 75.20617 74.95275 74.95275 74.70059 74.70059 74.70059
74.57498 74.44968 74.32469 74.07563  85.57237 90.40123 90.73760 90.73760
90.73760 90.73760 90.90648 91.07582 91.24564 90.90648 86.82135 80.69793
79.30393 78.62058 78.21484 77.81226 77.67876 77.41279 77.28032 76.88495
76.75383 76.75383 76.49260 76.36249  76.23270 76.23270 76.10325 75.97412
75.84532 75.71685  75.71685 75.71685 75.71685 75.46087 75.46087 75.46087
75.33336 75.20617 75.20617 75.20617 75.20617 75.20617 75.20617 75.07930
75.07930 75.07930 74.95275 74.95275  74.95275

I want to measure consecutive increases corresponding to irrigation and
consecutive decreases  corresponding to recharge
I wrote the following code and it does not calculate for each increment in
i?
also note that I choose to not use diff command in time series because I
 want also that "plateaux" corresponding to a minimum of 2 equal
consecutive values are accounted as positive differences=irrigations so
when x[i+1]==x[i] the difference y might be equal to the previous value xi

following the code i wrote

x<-ts(qWC1,start=1, end=61, frequency=1)
x[1]
plot(x, type="h", col = "green")
y<-rep(0,61)
for (i in 1:61) {
if (x[i+1] > x[i]){
    y[i]==x[i+1]-x[i]
} else if (x[i+1]==x[i]){
    y[i]=x[i+2]-x[i]
} else {
    y[i]==x[i+1]-x[i]
}

}
plot(y, type="h", col = "blueviolet")

Many thank
Makram

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list