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
151 views
in Technique[技术] by (71.8m points)

r - Group by function query

Hi guys i am new to R,

While i have attached screenshot of the df i am working with (https://i.stack.imgur.com/CUz4l.png), here is a short description

I have a data frame with a total of 7 columns, one of which is a month column, rest of the 6 columns are (integer) values and these also have empty rows

Need to summarise by count of all the 6 columns and group them by month

tried the following code: group_by(Month) %>% summarise(count=n(),na.omit())

get the following error:
Error: Problem with summarise() input ..2. x argument "object" is missing, with no default i Input ..2 is na.omit(). i The error occurred in group 1: Month = "1". Run rlang::last_error() to see where the error occurred.

Can someone please assist?

[head of data][1] (https://i.stack.imgur.com/stfoG.png)

> dput(head(Dropoff))
structure(list(Start.Date = c("01-11-2019 06:07", "01-11-2019 06:07", 
"01-11-2019 06:08", "01-11-2019 06:08", "02-11-2019 06:08", "02-11-2019 06:07"
), End.Date = c("01-11-2019 06:12", "01-11-2019 09:28", "01-11-2019 10:02", 
"01-11-2019 13:05", "02-11-2019 06:13", "02-11-2019 06:16"), 
    Month = structure(c(3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1", 
    "2", "11"), class = "factor"), nps = c(9L, 10L, 9L, 8L, 9L, 
    9L), effort = c(9L, 10L, 9L, 9L, 9L, 8L), knowledge = c(NA, 
    NA, 5L, NA, NA, 5L), confidence = c(5L, 5L, NA, NA, 5L, NA
    ), listening = c(NA, NA, NA, 5L, NA, NA), fcr = c(1L, 1L, 
    1L, 1L, 1L, 1L), fixing.issues = c(NA, NA, NA, NA, NA, NA
    )), row.names = c(NA, 6L), class = "data.frame")

id like the output to look something like this

Month count of nps count of effort
1 xxx xxx
2 xxx xxx
11 6 6

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

1 Answer

0 votes
by (71.8m points)

looks like the na.omit() causes problems in this case. Given that you want to count NA but not have them in any following sum, you might use

df[is.na(df)] = 0

and then

df %>% group_by(Month) %>% summarise(count=n())

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

...