I am a beginner in R but I would like to write a piece code that requires some R and data science knowledge.
I have a data frame with the following structure; t1 denotes 10 minutes time periods and 1 defines measurements.
t1 t2 t3 t4
1 0 0 0
1 1 1 1
0 1 1 1
0 1 1 1
1 0 1 1
I would like to identify the duration and starting point for each measurement. For example, there are two 10 min measurements starting at t1 (row 1 and row 5) and there are two 30 minutes measurements that start at t2 (row 3and row 4).
Output:
duration_minutes t1 t2 t3 t4
10 2 0 0 0
20 1 0 1 0
30 0 2 0 0
40 1 0 0 0
Is there a way to convert the counts into percentages?
df<-structure(list(t1 = c(1, 1, 0, 0,1),
t2 = c(0, 1, 1, 1,0), t3 = c(0, 1, 1, 1,1), t4 = c(0, 1, 1, 1,1)), row.names = c(NA,5L), class = "data.frame")
Use the COUNTIF function to count how many times a particular value appears in a range of cells.
You can use the =UNIQUE() and =COUNTIF() functions to count the number of occurrences of different values in a column in Excel.
To count the number of times a value occurs in a column of an R data frame, we can use table function for that particular column.
Using the COUNTIF function we can count the number of occurrences of each value in a column or range. The COUNTIF function counts the number of cells within a range comparing a particular condition. Syntax or generic formula of COUNTIF is as follows range: The range of cells you want to count
We first find an occurrence using binary search. Then we match toward left and right sides of the matched the found index. Time Complexity : O (Log n + count) where count is number of occurrences.
Customer Name and City are columns of text values and Salary for numbers values. This relation and data set are for practice purposes only. 1. Using COUNTIF function 2. Using SUM-EXACT functions 3. Using COUNT-IF functions 4. SUM-IF functions to count Number of Occurrences 5. Pivot Table
One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string.count () method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method. This method is very simple to implement.
I have solved it by first pivoting the data into the long format and then counting consecutive 1's to determine the duration of each measurement. Then I count how often each duration occurs per starting time and pivot back to the wider format to get the output that you described (I do not find a column t4 since it doesn't have any new measurements):
library(tidyr)
library(dplyr)
df %>%
mutate(rownr = 1:nrow(.)) %>%
pivot_longer(names_to = 'time', values_to = 'value', cols = 1:4) %>%
group_by(rownr, grp = cumsum(value == 0)) %>%
mutate(duration = 10 * cumsum(value)) %>%
filter(duration != 0) %>%
summarise(time = first(time),
duration = max(duration)) %>%
group_by(time, duration) %>%
count() %>%
pivot_wider(names_from = time, values_from = n, values_fill = 0) %>%
arrange(duration)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With