r - 在 R 中自动过滤测量数据

标签 r search dataframe filtering

我的问题是关于测量数据的自动过滤,因为我有几百个文件要处理。 文件结构如下:

test1 <- read.table("~/test1.txt",sep="\t",dec=".",skip=17,header=TRUE)

Number  Time.s      Potential.V Current.A
1       0.0000      0.060       -0.7653
2       0.0285      0.060       -0.7597
3       0.0855      0.060       -0.7549
.....
17      0.8835      0.060       -0.7045
18      0.9405      0.060       -0.5983
19      0.9975      0.061       -0.1370
20      1.0545      0.062        0.1295
21      1.1115      0.063        0.2680
......
8013    456.6555    0.066       -1.1070
8014    456.7125    0.065       -1.1850
8015    456.7695    0.063       -1.2610
8016    456.8265    0.062       -1.3460
8017    456.8835    0.061       -1.4380
8018    456.9405    0.060       -1.4350
8019    456.9975    0.060       -1.0720
8020    457.0545    0.060       -0.8823
8021    457.1115    0.060       -0.7917
8022    457.1685    0.060       -0.7481

我需要摆脱 Potential.V == 0.06 开头和结尾的额外行。我的问题是各种文件开头和结尾的行数不固定。

下一个限制是该文件包含多个测量值,因此我不能只删除 data.frame 中所有带有 0.06 的行。

我手动切割的那一刻,不是很优雅,但我不知道更好的解决方案:

test_b1 <- data.frame(test1$Number[18:8018],test1$Time.s[18:8018],test1$Potential.V[18:8018],test1$Current.A[18:8018])

我尝试使用像这样的迭代

for (c in 1:(length(test1))) {
    if (counter>1) & ((as.numeric(r[counter])- as.numeric(r[counter-1]))==1) {
       cat("Skip \n")}
}

但我没有找到可行的解决方案,因为我这边缺乏技能:/。

是否有 CRAN 上的模块或更优雅的方法来解决此类问题?

最好的问候

最佳答案

这是一个使用 rle 的方法:

filter.df <- function(df) {
    pot.rle <- rle(df$Potential.V)
    idx <- cumsum(pot.rle$lengths)
    val <- pot.rle$values
    chk <- ifelse(val[1] == 0.06 & val[length(val)] == 0.06, TRUE, FALSE)
    if (chk) {
        df[(idx[1]):(max(idx[1], idx[length(idx)-1])+1), ]
    }
}
filter.df(df)

#    Number   Time.s Potential.V Current.A
# 5      18   0.9405       0.060   -0.5983
# 6      19   0.9975       0.061   -0.1370
# 7      20   1.0545       0.062    0.1295
# 8      21   1.1115       0.063    0.2680
# 9    8013 456.6555       0.066   -1.1070
# 10   8014 456.7125       0.065   -1.1850
# 11   8015 456.7695       0.063   -1.2610
# 12   8016 456.8265       0.062   -1.3460
# 13   8017 456.8835       0.061   -1.4380
# 14   8018 456.9405       0.060   -1.4350

关于r - 在 R 中自动过滤测量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610648/

相关文章:

r - dbConnect with R 3.0 on Ubuntu 12.04 x64 -- as.integer(from) : cannot coerce type 'S4' to vector of type 'integer' 错误

search - VScode : Is there a way to hide symlink results from Goto file.。 (快速文件打开)在 VScode 中?和一般搜索?

Python - 计算给定文本中的单词

python - 从字典中提取键值作为数据框

python - 如何从 Dataframe 列形成元组列表

r - 如何递归合并列表列表的相应元素

r - 如何在 R 中找到具有值(每行)的最后一列?

r - 使用separate 来分割列中奇数个变量

python - 如何在二维数组中使用 numpy.searchsorted 进行矢量化

python - 如何检查一个数据框列在其他四个数据框的列中是否可用?