R Program

#1
I have a precipitation data"daily" and I formed the following index DRI from the data

\(
\begin{tabular}{|l|cr|}
\hline
Daily Rainfall(mm) & DRI \\
\hline
0-4.9 & 0 \\
5-9.9 & 1 \\
10-14.9 & 2 \\
so the general formula is
5n-5n+4.9 & n\\
\hline
\end{tabular}
\)

I have the following code:

Code:
 PCP <- c(0, 4.9, 5, 9.9, 10, 14.9, 15)
seq2max <- seq(0,max(PCP)+70,5)
result <- data.frame(min=seq2max,max=seq2max+4.9,DRI=seq_along(seq2max)-1)
result$DRI
result$DRI^2
Now I have the daily dates for the precipitation and I want to specify the dates with precipitation >= 10 and
\( \sum_{d = 1}^30 {N_d * DRI^2_d} \)
then sum over 30 or 31 days for N*DRI^2 where N is the day with pcp>=10 (i.e get the monthly index)

for example I have days 1,2,3,4,5,6,7,8,9,10 and so on up to 30 and the days with precipitation >=10 is days 1 with value 10 mm, day 4 with value 15 mm so I want to multiply 1 by the corresponding index which is DRI^2 =4 and day 4 multiplied by 9 and so one and sum over those days

my questions are:
1) how to select the days with precipitation >=10
2) how to form such sum

Thanks in advance
 
Last edited:

trinker

ggplot2orBust
#2
Marwah :welcome:

When you're posting code, dataframes or computer output it's helpful to wrap this information in code tags by:
  1. either clicking the pound (#) sign icon or
  2. wrap with [NOPARSE]
    Code:
    some code
    [/NOPARSE]

which produces:
Code:
some code
For more see this (LINK)
 

trinker

ggplot2orBust
#3
Also what does:

then sum over 30 or 31 days for N*DRI^2 where N is the day with pcp>=10 (i.e get the monthly index)
mean?

Maybe if you give an example of what you expect the output(s) to look like this can be more helpful.
 
#5
the output should be a number for each month i.e I should have 12 numbers

since I have daily data, I want to sum the days to form months.
 
Last edited:

Dason

Ambassador to the humans
#9
I don't know how to use this in answering my question,, sorry i'm new to R
He was just giving an example of a case where somebody provides example input and expected output. That's pretty much it - it's easiest if we know what you expect to get from some example input....
 
#10
He was just giving an example of a case where somebody provides example input and expected output. That's pretty much it - it's easiest if we know what you expect to get from some example input....
Well what I'm expecting is 12 numbers for each month which is the MRI " monthly rainfall index" I can't type the code as I don't know how to do it yet !!
 
#11
I will try my best here is some syntax of what I want:
Code:
data <-read.table("C:/Users/hp/Desktop/data daily 2001-2020", header=F)
 tmp <- rep(c(TRUE, FALSE), dim(data)[1]/2)
 date <- data[tmp,1]
date <- as.Date(as.character(date), "%Y%m%d")
 pcp <- data[!tmp,1]
 length(pcp)==length(date) #[1] TRUE
day <- as.numeric(format(as.Date(date), format="%d"))
so what I want is as follows:

if PCP>=10 then day1 = number of the day which is %d"
so that I get a list of the days with pcp>=10

MRI= sum( day1*DRI^2) where the sum here is for each month I mean if January has 2 days with pcp>=10 then the sum is over those 2 days
so what I get is Jan MRI= number,
FEB MRI=number and so on up to DEC.

IS that Clear??
 
Last edited:

trinker

ggplot2orBust
#13
@marwah,

I think that maybe English is not your first language and it is my only language. It makes understanding what you want difficult. Also most R users are not familiar with your field. The way to get around this is to provide two data sets. One to show the data you have. And then a second to show the data you want. This makes figuring out what you want much more easy.

Here's an example of what you may try:

I have this data:
Code:
              names  mpg cyl disp
1         Mazda RX4 21.0   6  160
2     Mazda RX4 Wag 21.0   6  130
3        Datsun 710 22.8   4  108
4    Hornet 4 Drive 21.4   6  258
5 Hornet Sportabout 18.7   8  360
I want cyl to be greater than 4 and disp to be greater than 150

I want this data:
Code:
              names  mpg cyl disp
1         Mazda RX4 21.0   6  160
4    Hornet 4 Drive 21.4   6  258
5 Hornet Sportabout 18.7   8  360
 
#14
The following is the data I have run the code:
Code:
data <-read.table("C:/Users/hp/Desktop/data daily 2001-2020", header=F)
 tmp <- rep(c(TRUE, FALSE), dim(data)[1]/2)
 date <- data[tmp,1]
date <- as.Date(as.character(date), "%Y%m%d")
 pcp <- data[!tmp,1]
 length(pcp)==length(date) #[1] TRUE
day <- as.numeric(format(as.Date(date), format="%d"))
then Run the following:
Code:
PCP <- c(0, 4.9, 5, 9.9, 10, 14.9, 15)
seq2max <- seq(0,max(PCP)+70,5)

result <- data.frame(min=seq2max,max=seq2max+4.9,DRI=seq_along(seq2max)-1)

result$DRI
result$DRI^2
 
#15
Well, yes English is not my First Language, it is my second :), my Field is Environmental Statistics :)
Anyway,
lets take it step by step:
1) I want to get the days (column) with precipitation>=10
2)I want to multiply the day with the corresponding DRI^2 , according to the schedule above
3) I want to sum over the days with pcp>=10 to get the monthly index
 

trinker

ggplot2orBust
#17
I now get what you want. You haven't provided data that looks like what you're talking about. You're giving min and max and extraneous stuff that distracts from your problem. When you ask a question provide a minimal reproducible example with emphasis on minimal. Don't include extra stuff as it's a distraction. The results data frame makes no sense and no where have I seen days in your dataframe. You said where N is the days but I never see N? Let me create for you the data set you should have created so you learn how to do this for your self:


Code:
dat <- data.frame(
    day = 1:7, 
    PCP = c(0, 4.9, 5, 9.9, 10, 14.9, 15), 
    DRIsqr = (1:7)^2)

dat2 <- dat[dat$PCP >= 10, ]
dat2$PCPxDRIsqr <- dat2[, 1] * dat2[, 3]
sum(dat2$PCPxDRIsqr)
PS When you write:

marwah said:
Code:
data <-read.table("C:/Users/hp/Desktop/data daily 2001-2020", header=F)
 tmp <- rep(c(TRUE, FALSE), dim(data)[1]/2)
 date <- data[tmp,1]
date <- as.Date(as.character(date), "%Y%m%d")
 pcp <- data[!tmp,1]
 length(pcp)==length(date) #[1] TRUE
day <- as.numeric(format(as.Date(date), format="%d"))
we can't see your data. That's really what we need. You code is secondary in importance to what the data looks like.
 

JesperHP

TS Contributor
#18
I'm not really able to follow youre code. But I'm thinking if you have youre pcp data in a matrix you could do a logical matrix and simply nullify data with pcp not satisfying condition... then summing over rows or columns whatever is relevant...

code example:
Code:
M=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)
 Q=M>3
 Q*M