Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

ggplot2 - I want to obtain the values of reliability != 1 by day in R

I want to know how many values are different from 1 in my dataframe.

My input is like:

simulation, day, reliability
1, 1, 0.999
1, 2, 0.999
1, 3, 0.999
2, 1, 1
2, 2, 0.999
2, 3, 1
3, 1, 0.98
3, 2, 0.98
3, 3, 1

And the output should be like:

day, counter
1, 2
2, 3 
3, 1

Many thanks in advance!

question from:https://stackoverflow.com/questions/65843381/i-want-to-obtain-the-values-of-reliability-1-by-day-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can take sum of logical values to count how many of them are different than 1 for each day.

In base R :

aggregate(reliability~day, df, function(x) sum(x != 1))

dplyr :

library(dplyr)
df %>%group_by(day) %>% summarise(counter = sum(reliability != 1))

and data.table:

library(data.table)
setDT(df)[, .(counter = sum(reliability != 1)), day]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...