r - 如何创建具有数据间隙的数据集的子样本
问题描述
将潮汐数据分成更小的数据集
期望的结果是确定数据集是否包含 NA,如果是,则将数据集分成多个数据集,省略没有水位数据的部分(高度..英尺)。数据包括时间..分钟。从 0 和相关水位开始以 15 分钟为增量
我的目标是自动化创建数据子样本的过程。
我是 R 的新手,非常感谢所有帮助!
install.packages(dplyr)
library(dplyr)
fiftn.min <- read.table('C:/Temp/WL.csv',header=T,sep=',')
# View rows where height is NA (many missing rows of data)
fiftn.min[is.na(fiftn.min$height..ft.),]
# Plot to visually inspect data, plot shows three distinct complete sets of data
# and two data gaps.
plot(fiftn.min, pch = 20, col = "deepskyblue4", cex = .05,
main = "ENTIRE SAMPLE",
xlab = "Time (Min) 15 Minute Increments",
ylab = "Water Level (ft)")
# Inspect the table to investigate instances of NAs
# Visual inspection of the table shows that data gaps begin at time..min. =
# 124560 and time..min. = 193545
View(fiftn.min[is.na(fiftn.min$height..ft.),])
# Use the filer function to separate the segments of data without observations
# Manually inspect to determine the range of data sub samples
par(mfrow = c(3,1))
s1 <- filter(fiftn.min, time..min. < 124560)
plot(s1, pch = 20, col = "deepskyblue4", cex = .05,
main = "SAMPLE 1",
xlab = "Time (Min) 15 Minute Increments",
ylab = "Water Level (ft)")
s2<- filter(fiftn.min, time..min. >132000 & time..min. <193545)
plot(s2, pch = 20, col = "deepskyblue4", cex = .05,
main = "SAMPLE 2",
xlab = "Time (Min) 15 Minute Increments",
ylab = "Water Level (ft)")
s3 <- filter(fiftn.min, time..min. > 204525)
plot(s3, pch = 20, col = "deepskyblue4", cex = .05,
main = "SAMPLE 3",
xlab = "Time (Min) 15 Minute Increments",
ylab = "Water Level (ft)")
解决方案
不确定这是您要查找的内容 - 一个简单的数据示例可能会有所帮助。
我制作了一些示例数据,包括 atime
和height
列,将一些值设置height
为缺失 ( NA
):
library(dplyr)
set.seed(123)
df <- data.frame(
time = 1:20,
height = sample(20)
)
df$height[c(5:7, 13:15)] <- NA
生成的 data.frame 如下所示:
time height
1 1 15
2 2 19
3 3 14
4 4 3
5 5 NA
6 6 NA
7 7 NA
8 8 11
9 9 5
10 10 4
11 11 18
12 12 9
13 13 NA
14 14 NA
15 15 NA
16 16 7
17 17 16
18 18 1
19 19 8
20 20 13
然后,您可以添加一列来指示数据集的开始位置(当 的值height
可用但height
缺少前一行时):
df$start <- !is.na(df$height) & is.na(lag(df$height))
然后,您可以创建组来枚举数据子集:
df$group <- cumsum(df$start)
最后用于split
拆分 data.frame (最终结果是 a list
):
df |>
na.omit(height) |>
split(~group)
输出
$`1`
time height start group
1 1 15 TRUE 1
2 2 19 FALSE 1
3 3 14 FALSE 1
4 4 3 FALSE 1
$`2`
time height start group
8 8 11 TRUE 2
9 9 5 FALSE 2
10 10 4 FALSE 2
11 11 18 FALSE 2
12 12 9 FALSE 2
$`3`
time height start group
16 16 7 TRUE 3
17 17 16 FALSE 3
18 18 1 FALSE 3
19 19 8 FALSE 3
20 20 13 FALSE 3
注意:您可以将上述内容分配给第二个 data.frame,然后在需要时提取特定的组/子集。例如,如果分配给df2
并想要组 2,则可以使用df2[[2]]
.
编辑:对于有关情节的后续问题,您可以尝试以下操作:
首先,将结果存储为my_list
:
my_list <- df |>
na.omit(height) |>
split(~group)
然后,您可以为所有绘图创建自定义函数 , plot_fun
,指定您将使用前 2 列数据。该函数还可以包含num
作为参数来指定哪个样本。
plot_fun <- function(dat, num) {
plot(dat[,1:2], pch = 20, col = "deepskyblue4", cex = .05,
main = paste("SAMPLE", num),
xlab = "Time (Min) 15 Minute Increments",
ylab = "Water Level (ft)")
}
然后,您可以使用以下命令创建所有图mapply
:
mapply(plot_fun, dat = my_list, num = names(my_list))
此外,您可能想看看ggplot2
未来的情节。
推荐阅读
- typescript - 引入两个别名的模拟导入
- flutter - 如何在颤动中对多个小部件使用相同的键?
- php - 如何在 WooCommerce 中获取用户的活动订单 ID 和订单状态
- reactjs - 如何使用参数调用 React Context 函数
- java - 如何将文件夹构建到 spring boot gradle 项目的 .jar 根目录中?
- javascript - 来自 Firebase 的未定义道具
- javascript - 如何让我的图像滑块褪色?(没有jQuery)
- batch-file - 从 Jenkins Credential 获取密码对象以推送到将映射网络驱动器的批处理文件
- html - 如何使在 Web 应用程序上创建的 div 出现在父容器的左上角?
- linux - 命令完成时退出 Plink