This question already has answers here :
Here are two ways to do it. One of them is almost as @FALL Gora, but the other is from base R
# these two steps are assuming you have data.table
# modify them accordingly if you have data.frame
data <- data[order(Name, Date)]
data[is.na(Success), Success := 0]
### tapply
data$past_success <- unlist(with(data, tapply(Success, Name, cumsum)))
### data.table
data[, past_success_dt := cumsum(Success), by = Name]
data
Name Success Date past_success past_success_dt
1: David 1 2017 1 1
2: David 1 2018 2 2
3: Peter 0 2004 0 0
4: Peter 1 2005 1 1
5: Peter 0 2010 1 1
6: Peter 1 2015 2 2
7: Peter 1 2016 3 3
8: Peter 0 2017 3 3
How to Get the Frequency Table of a Categorical Variable as a Data , Introduction One feature that I like about R is the ability to access and manipulate the I recently needed to get a frequency table of a categorical variable in R, and I how the count() function in the 'plyr' package can achieve this goal. of functions to subset the output based on your desired condition. It is now easy to add the frequency of the categorical data to the original Data Frame x. Comparison The table() function is really useful as a quick summary and, with a little work, can produce an output similar to that given by the count() function.
Try this data table approach:
library(data.table)
data <-data.table(Name = rep(c("David","Peter","David","Peter"), c(1,2,1,4)),
Success = c(1,0,1,1,1,0,1,NA),
Date = c(2018,2017,2016,2017,2015,2010,2005,2004)
)
data <- data[order(Date)]
data[Success == 1,"Past Success":= cumsum(Success), by = 'Name']
How to get a frequency count based on two columns (variables) in , How do I get a frequency count based on two columns (variables) in an R dataframe? To get a 2-way frequency table (i.e. a frequency table of the counts of a data set as divided by 2 categorical variables), you can display it in a cross-tabulated format or in a list format. In R, the xtabs() function is good for cross-tabulation. Let’s use the “mtcars” data set again; recall that it is a built-in data set in Base R.
For the record: a dplyr approach for a dataframe
library(tidyverse)
data<-data%>%
arrange(Name, Date) %>%
group_by(Name) %>%
mutate(Success = replace_na(Success, 0),
PastSuccess = cumsum(Success))
data
> data
# A tibble: 8 x 4
# Groups: Name [2]
Name Success Date PastSuccess
<fct> <dbl> <dbl> <dbl>
1 David 1 2017 1
2 David 1 2018 2
3 Peter 0 2004 0
4 Peter 1 2005 1
5 Peter 0 2010 1
6 Peter 1 2015 2
7 Peter 1 2016 3
8 Peter 0 2017 3
Counting and aggregating in R, I'll post about that soon. I often want to count things in data frames. For example, let's say my antimatter equivalent Llib and I have been drinking� optional variable to weight by - if this is non-NULL, count will sum up the value of this variable for each combination of id variables. Details Speed-wise count is competitive with table for single variables, but it really comes into its own when summarising multiple dimensions because it only counts combinations that actually occur in the data.
Frequency tables, Category frequencies for one variable Counting non-existent categories Contingency tables for two or more variables Conditional relative frequencies. Frequency Table for a Single Variable. Calculates absolute and relative frequencies of a vector x. Continuous (numeric) variables will be cut using the same logic as used by the function hist. Categorical variables will be aggregated by table. The result will contain single and cumulative frequencies for both, absolute values and percentages.
[PDF] Working with categorical data with R and the vcd and , Using vcdExtra version 0.7-1 and vcd version 1.4-4; Date: 2017-09-29. Abstract. This tutorial R: Visualizing and Modeling Techniques for Categorical and Count Data (Friendly and Meyer. 2016). 6.1 Spine and conditional density plots . . 33 represent frequency and contingency tables involving categorical variables. 1� You can easily generate a pie chart for categorical data in r. Look at the pie function. Along the same lines, if your dependent variable is continuous, you can also look at using boxplot categorical data views (example of how to do side by side boxplots here). That concludes our introduction to how To Plot Categorical Data in R. As you can see
Count/tally observations by group — tally • dplyr, Source: R/count-tally. If the data is already grouped, count() adds an additional group that is removed afterwards. These functions are to tally() and count() as mutate() is to summarise() : they add an additional column rather than collapsing � A short post about counting and aggregating in R, because I learned a couple of things while improving the work I did earlier in the year about analyzing reference desk statistics. I’ll post about that soon. I often want to count things in data frames.
Comments Hi UseR, can you provide us with a minimum reproducible example? I would like to copy and paste the code to create your data frame (or a subset of it) into my R session, and be able to reproduce what you're getting, and then recommend a solution. Thanks :) can you provide sample data? Look into the rle()
function @FALLGora: Lines 6 and 7 of my question are actually a table of sample data and output. It is not showing correctly in columns. Is there a way to fix it? I copy pasted from an excel table This might also be helpful: stackoverflow.com/questions/44665944/… It generates NAs for three columns. But thanks it is a good way to code it. NAs are values that are suppose to be zero. you can update them by doing after: data[ ,"Past Success":= ifelse(is.na(`Past Success`),0,`Past Success`)]