Create a new column with intervals in R - r

I am looking for a quick solution how to create a new column in a data frame (interval) by taking into account the output of the time column.
My dummy column
time <- c(7.1,8.2,9.3,10.4,11.5,12.6,50.9)
df <- data.frame(time)
df
My desired output
Based on the info in the TIME column, I would like to determine the interval. In my example, whatever comes between 0.0-10.0 (including 10) equals interval 10. The intervals are grouped by 10, as you see. So whatever comes between 10.0-20.0 will be assigned to interval 20 and so on and so forth.
Any hints on how to get the new column with intervals would be highly appreciated. Thanks

Moved comments to an answer. You can use integer division to do this. You divide the time column by 10 as this is the interval you are looking for. Add 1 and multiply by 10 to get the results.
df["interval"]<-(df$time %/% 10 + 1) * 10

Related

Writing a function to give an interval where the values are greater than a certain number

I'm trying to write a function where I find the number of times the value in a data frame is above a certain number x (in this case, 3). Basically, the data start from 1.0, increase, then go below 1.0 (in a span of about 150 data points). I want the function to return to me the number of times the values are above this threshold. I'm fairly new to R and am just confused on how to go about this. Any help is appreciated. Thank you!
If your data frame is called df then sum(df$x>3) will return the number of rows of df where x is greater than 3.
If there are missing values in x and you want to ignore them then use sum(df$x>3, na.rm=TRUE).

R multiply values from different rows

I have the following data frame in R:
df <- data.frame(time=c("10:01","10:05","10:11","10:21"),
power=c(30,32,35,36))
Problem: I want to calculate the energy consumption, so I need the sum of the time differences multiplied by the power. But every row has one timestamp, meaning I need to do subtraction between two different rows. And that is the part I cannot figure out. I guess I would need some kind of function but I couldn't find online hints.
Example: It has to subtract row2$time from row1$time, and then multiply it to row1$power.
As said, I do not know how to implement the step in one call, I am confused about the subtraction part since it takes values from different rows.
Expected output: E=662
Try this:
tmp = strptime(df$time, format="%H:%M")
df$interval = c(as.numeric(diff(tmp)), NA)
sum(df$interval*df$power, na.rm=TRUE)
I got 662 back.

Adding to a row based upon values from another variable

I'm very new to R and currently working through data from my lab. I have a dataframe with a good amount of variables- two of these variables are Sample and Time. Each sample records a maximum of 10 minutes of observations, then restarts at 0 again for the next sample. I.e., sample 1 correctly displays the timestamps from 0 minutes to 10 minutes. However, upon going above 10 minutes of observations, the Time column will display 0, and the Sample column will display 2. Therefore, each time value in sample 2 observations should be the time displayed plus 10, each time value in sample 3 should be the time displayed plus 20, etc etc. What would be the best way to go about this? Sorry again if I don't have any of the jargon down, I just started learning r.
Without knowing for sure where the column that starts with 9.314... I cannot give an exact answer.
Is there a way for you to add something like this:
df$Time <- df$Time + (df$Sample - 1) * 10
My idea is to take the Time column and add
(1 - 1) * 10 = 0 for Sample 1
(2 - 1) * 10 = 10 for Sample 2
etc

to give a column's data equal values divide by the Average (Mean) numbers in the series in R

An easy function (for everyone) to give equal Values divide by the Average (Mean) instead of writing it out all the time every time I change the amount. In this example I have 5. But I might want more or less. Thanks. All add up to 1. I know I didn't explain it properly, but I hope you understand
Amount <- c(Coat=1/5,Boat=1/5,Shop=1/5,Car=1/5,Bike=1/5)
Lets say I have 5 series in a Column. I want the values in the column to be all equal (1/5). I want the Column to add up to 1.
sum(Amount) = 1
I don't know whether I understood correctly, but I think you want to transform a vector to retain the proportions but have a sum of exactly 1. The way to do this would be to divide the vector by its sum:
> amounts = c("car"=2,"bike"=2,"ship"=1)
> amounts = amounts/sum(amounts)
> amounts
car bike ship
0.4 0.4 0.2
Is this what you were looking for?

time averaged data in R

I am given a dataset of 10 hour length (36.000 time points, 1 second=1 time point). For my further analysis I am supposed to use 10min averaged data which would equal 600 time points.
So do I understand this right that I have to take the average of the first 600 time points and thats my new time point and then of the next 600 and so on? Which means I end up with a time series of length 60.
How do I do that with R? I thought
xF<-filter(x, 600, sides = 2)
would be the required function but it just changes the values on the y axis.
If your dataset is ordered, you could just create a grouping variable and use tapply:
# simulate data
x <- rnorm(36000)
# create a group variable
group <- factor(rep(1:(36000/600),each=600))
# compute mean for each slice of 600 data point
mean_by_10min <- tapply(x,group,mean)
It is hard to help you without having your data
However, the command looks most likely like
aggregate(iris$Petal.Length,by=list(iris$Petal.Width%/%0.5),mean)
where you need to replace iris$Petal.Length by your values, iris$Petal.Width by the timestamps and 0.5 by 600
Filtering doesn't aggregate your data as I understand your question, hence, you would end up with as many time points.

Resources