I would like to count how many rows there are per type
if they meet the condition x == 0
. Sort of like a group by in SQL
Here is an example of the data
type x
search 0
NULL 0
public 0
search 1
home 0
home 1
search 0
Given your data is structured as a data frame, the following code has a better running time than the answers given above:
You can test your run time using:
In my case, the running time was about 15 times faster, with 1 million rows.
You could also use the sqldf package:
which gives the following result:
Given the data frame,
df=data.frame(type=c('search','NULL','public','search','home','home','search'),x=c(0,0,0,1,0,1,0))
If you want to know how many of each value in column 1 have a value in column 2 of zero then you can use:
table(df)[,1]
as long as you are only working with 1's and 0's to get the answer:
You could also do this with the
dplyr
package:which gives:
I am assuming that you want to find the number of rows when a particular condition (when a variable is having some value) is met.
If this is the case, then I suppose you have "x" as a variable represented in a column. "x" can take multiple values. Suppose you want to find how many rows are there in your data when x is 0. This could be done by:
'data' is the object name for your dataset in R
EDIT:
I am seeing your edited dataframe now. You could use this to solve your problem: