Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count the total number of occurrences at time step t of an element?

Tags:

dataframe

r

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") 

                                                                                  
like image 935
user11418708 Avatar asked Sep 15 '20 19:09

user11418708


People also ask

How do I count the number of times a value is repeated in Excel?

Use the COUNTIF function to count how many times a particular value appears in a range of cells.

How do you use Excel to count number of occurrences in a column?

You can use the =UNIQUE() and =COUNTIF() functions to count the number of occurrences of different values in a column in Excel.

How do you count the number of times a value appears in a column in R?

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.

How to count the number of occurrences of each value?

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

How do you find the number of occurrences of an index?

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.

How to count number of occurrences and salary in Excel?

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

How to count the number of occurrences in a string in Python?

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.


1 Answers

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)
like image 162
pieterbons Avatar answered Sep 27 '22 17:09

pieterbons