Count observations greater than a particular value
Try using logical test and then sum over the values meeting the condition
sum(output$V4 > 2000)
counting observations greater than 20 in R
The data is character in this dataframe and not numeric. When numbers are characters weird things happen.
"2" > "13"
#[1] TRUE
Change the data to numeric before using sum
.
df[-1] <- lapply(df[-1], as.numeric)
sum(df[,-1] > 20, na.rm=TRUE)
#[1] 8
tidyverse: finding number of observations greater than multiple values
You can use summarize
directly:
dt1 %>%
summarise(X1 = sum(X >= 5),
X2 = sum(X >= 7))
# A tibble: 1 x 2
X1 X2
<int> <int>
1 6 4
Count observations with certain value in a group?
df <- df %>%
group_by(Year, Month) %>%
mutate(XGreaterThanYCount = sum(X > Y))
number of values in a list greater than a certain number
You could do something like this:
>>> j = [4, 5, 6, 7, 1, 3, 7, 5]
>>> sum(i > 5 for i in j)
3
It might initially seem strange to add True
to True
this way, but I don't think it's unpythonic; after all, bool
is a subclass of int
in all versions since 2.3:
>>> issubclass(bool, int)
True
How to determine how many entries exceed a certain value
If df
is your dataset:
df[df$cumula > 3000,]
will give you the entries that exceed 3000.
If you only need to count how many, you can do
sum(df$cumula > 3000)
counting the number of values greater than 0 in R in multiple columns
We can use
colSums(myDF[c("L2", "L3", "L4")] > 0)
How to count observations matching the values of a vector of characters
To count matching or non-matching elements, you can use
num_foods <- nrow(mydf[!str_detect(mydf$Product, non.food),])
num_non_foods <- nrow(mydf[str_detect(mydf$Product, non.food),])
You can see, that num_foods == 8
and num_non_foods == 8
, so your code seems to do what it should.
data
mydf <- structure(list(id = 1:16, Product = c("Pizza", "Cleaning Product",
"Chocolate", "Fruit", "Red Meat", "Cleaning Product", "Bracelet",
"Trucker Hat", "Shirt", "Shirt", "Chicken Breast", "Chocolate",
"Cereal", "Fruit", "Cleaning Product", "Trucker Hat"), price = c(2,
3.5, 1, 1, 2.5, 3.5, 3, 5, 15, 20, 2.5, 1, 2, 1, 3.5, 4), place = c("Supermarket",
"Supermarket", "Supermarket", "Little Store", "Supermarket",
"Supermarket", "Little Store", "Gas Station", "Supermarket",
"Supermarket", "Little Store", "Gas Station", "Gas Station",
"Little Store", "Supermarket", "Supermarket")), row.names = c(NA,
-16L), class = "data.frame")
Related Topics
Checking If Date Is Between Two Dates in R
How to Coerce a List Object to Type 'Double'
Controlling Order of Facet_Grid/Facet_Wrap in Ggplot2
Stacked Bars Within Grouped Bar Chart
How to Convert Integer into Categorical Data in R
Function to Split a Matrix into Sub-Matrices in R
R's Read.CSV Prepending 1St Column Name with Junk Text
Add a Column with Count of Nas and Mean
Count How Many Values in Some Cells of a Row Are Not Na (In R)
R Matrix to Rownames Colnames Values
Greek Letters, Symbols, and Line Breaks Inside a Ggplot Legend Label
Group by Two Columns in Ggplot2
Data Input via Shinytable in R Shiny Application