【问题标题】:R data reading and PlottingR 数据读取和绘图
【发布时间】:2017-05-13 14:24:18
【问题描述】:

我只是水文建模 R 的学习者。我有 readLines 读取的平面文件,我想 grep 并绘制值。这些值以数千个时间间隔为单位,但算法只是根据 0 到 9 的时间绘制值。这里是代码

library(stringr)

x <- readLines("D:/Rlearning/Mohsin HYDRUS/Mohsin practice/Balance.out")

## Find all lines with [T]
a <- grep("[T]", x, value = T)
### Find all the lines that also say Time in that subset
b <- grep("Time", a , value = T)
#### Remove the first 2 lines in b (they didn't have a watbalR associated with it)
c <-  b[-c(1,2)]
### Find all WatBalR lines
d <- grep("WatBalR", x, value = T)
#### Put them together in a dataframe
data <- data.frame(time =c, watbalr = d)
#### Still need to filter numbers from each line don't know how to do it off the top of my head should be able to google it though like "pulling numbers from a string in R" or something.
#### Hopefully this helps

# Extract the numeric values from the two character vectors
# Use sub to omit all the characters before the first digit
times   <- sub("^.+?(\\d)", "\\1", c)
WatBlaR <- sub("^.+?(\\d)", "\\1", d)

# convert the characters to numbers
times   <- as.numeric(times)
WatBlaR <- as.numeric(WatBlaR)

# plot 
plot(x = times, y = WatBlaR)

当我将字符转换为数字时,问题就开始了,一位专家几天前在这里做过这个,但它没有产生准确的要求。

我是新手,我想问这个问题,但无法。我希望我能从这个网站学到更多。

【问题讨论】:

  • 您能否详细说明预期的输出是什么?从问题中不清楚您对 我想为 WatBalR 制作时间和第二列的值是什么意思。您是否正在寻找聚合这些的方法?
  • 非常感谢,K..pradeep。你能把我的概念清楚的材料寄给我吗?你太棒了。上周我被困在里面。我正在优化这个模型。我的第一个任务是 R 中的简单优化技术。请将我可以执行的包名称和材料发送给我。非常非常非常非常感谢。现在我将处理完整的文件。
  • 请不要滥用评论系统。如果您想提供更多信息,请编辑您的问题。
  • 按问题左下角的edit 按钮。或click here.

标签: r database optimization plot split


【解决方案1】:

您需要从提供的字符串中提取数值。

# Extract the numeric values from the two character vectors
# Use sub to omit all the characters before the first digit
times   <- sub("^.+?(\\d)", "\\1", c)
WatBlaR <- sub("^.+?(\\d)", "\\1", d)

# convert the characters to numbers
times   <- as.numeric(times)
WatBlaR <- as.numeric(WatBlaR)

# plot 
plot(x = times, y = WatBlaR)

一个建议:不要使用c 作为变量名。 c 是将值组合成向量或列表的函数。使用c 作为变量可能会导致问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2021-03-19
    • 2020-12-13
    • 2012-04-02
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多