组中所有不适用的投递ID

编程入门 行业动态 更新时间:2024-10-27 12:27:17
本文介绍了组中所有不适用的投递ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下代码准备了一些数据:

I have some data prepared using the below code:

# # Data Preparation ---------------------- library(lubridate) start_date <- "2018-10-30 00:00:00" start_date <- as.POSIXct(start_date, origin="1970-01-01") dates <- c(start_date) for(i in 1:287) { dates <- c(dates, start_date + minutes(i * 10)) } dates <- as.POSIXct(dates, origin="1970-01-01") date_val <- format(dates, '%d-%m-%Y') weather.forecast.data <- data.frame(dateTime = dates, date = date_val, id = 'GH1', radiation = runif(288)) weather.forecast.data$radiation[(weather.forecast.data$id == 'GH1') & (weather.forecast.data$date == '30-10-2018')] = NA

我的任务是从 weather.forecast.data 过滤行,其中对于id和date的每个唯一实例,所有辐射值都将丢失。

My task is to filter rows from the weather.forecast.data where all radiation values are missing for each unique instance of id and date.

我有使用 data.table 编写的代码:

library(data.table) setDT(weather.forecast.data) weather.forecast.data[, dateid := paste(date, id, sep = "__")] weather.forecast.data[, is_all_na := all(is.na(radiation)), dateid] weather.forecast.data = weather.forecast.data[!(is_all_na), !c('dateid', 'is_all_na'), with = FALSE]

我正在尝试使用 dplyr 函数和管道操作以提高可读性:

I am trying to use dplyr functions and pipe operations to make it better readable:

library(dplyr) weather.forecast.data %>% mutate(dateid = paste(date, id, sep = "__")) %>% group_by(dateid) %>% summarise(is_all_na = all(is.na(radiation))) %>% filter(is_all_na) %>% select(dateid)

我能够检索 id 都丢失了。但是,我无法从原始数据中删除 id 。

I am able to retrieve the id with all missing. But, I am unable to remove the id from the original data.

推荐答案

无需一起粘贴 列,则可以 group_by 多列

No need to paste columns together, you can group_by multiple columns

library(dplyr) weather.forecast.data %>% group_by(date, id) %>% filter(!all(is.na(radiation)))

这将删除行其中全部辐射 是每个<$ c的 NA $ c>日期和 id 。

This will drop the rows where all the radiation is NA for each date and id.

更多推荐

组中所有不适用的投递ID

本文发布于:2023-10-26 10:10:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用   组中   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!