R-project Zoo 对象:如何删除数据非数字的行

发布于 2024-10-29 18:43:35 字数 807 浏览 4 评论 0原文

我有一个 csv 文件,其中有 2 列,以逗号分隔 - 第一列是日期,后跟假定的数字数据。

我通过 read.csv 函数将数据加载到 R 中,该函数将数据存储在具有 2 列的 data.frame 对象中。我执行一些操作,将对象转换为动物园对象,并将索引设置为日期。现在该对象有一列,假设是数字数据和日期索引。

问题是数据中的字符串“ND”随机分散。我只想提取动物园对象中不包含“ND”的那些行。

yr2 是动物园关注的对象。

示例:

03/15/2011 0.63 
03/16/2011 0.58 
03/17/2011 0.60 
03/18/2011 0.61 
03/21/2011 0.67 
03/22/2011 ND 
03/23/2011 0.69 
03/24/2011 0.72 
03/25/2011 0.79 
03/28/2011 0.81 
03/29/2011 0.81 
03/30/2011 0.80 
03/31/2011 0.80 

我已经尝试过以下操作:

> yr2[!="ND"]  
Error: unexpected '!=' in "yr2[!="  
> yr2[yr2[!="ND"]]  
Error: unexpected '!=' in "yr2[yr2[!="  
>   
> yr2[!is.character(yr2)]  
Data:  
character(0)  

Index:
Data:
named character(0)

Index:
integer(0)

我将非常感谢一些指导。谢谢。

I have a csv file that has 2 columns separated by a comma - the first is the date followed by what is suppose to be numeric data.

I load the data into R via the read.csv function which stores the data in a data.frame object with 2 columns. I perform some manipulations to transform the object into a zoo object with the index set to the date. So now the object has one column, which is suppose to be numeric data and the date index.

The problem is the data has the character string "ND" randomly scattered about. I want to extract only those rows of the zoo object that do not contain "ND".

yr2 is the zoo object of concern.

Example:

03/15/2011 0.63 
03/16/2011 0.58 
03/17/2011 0.60 
03/18/2011 0.61 
03/21/2011 0.67 
03/22/2011 ND 
03/23/2011 0.69 
03/24/2011 0.72 
03/25/2011 0.79 
03/28/2011 0.81 
03/29/2011 0.81 
03/30/2011 0.80 
03/31/2011 0.80 

I have tried the following:

> yr2[!="ND"]  
Error: unexpected '!=' in "yr2[!="  
> yr2[yr2[!="ND"]]  
Error: unexpected '!=' in "yr2[yr2[!="  
>   
> yr2[!is.character(yr2)]  
Data:  
character(0)  

Index:
Data:
named character(0)

Index:
integer(0)

I would greatly appreciate some guidance. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-11-05 18:43:35

在将有问题的“ND”数据转换为 zoo 对象之前解决它是否有意义? ND 是否代表“无数据”,即应解释为 NA?

txt <- "03/15/2011 0.63 
03/16/2011 0.58 
03/17/2011 0.60 
03/18/2011 0.61 
03/21/2011 0.67 
03/22/2011 ND 
03/23/2011 0.69 
03/24/2011 0.72 
03/25/2011 0.79 
03/28/2011 0.81 
03/29/2011 0.81 
03/30/2011 0.80 
03/31/2011 0.80"

#If ND == NA
dat <- read.table(textConnection(txt), header = FALSE, na.strings = "ND") 

#if not
dat <- read.table(textConnection(txt), header = FALSE) 

dat[dat$V2 != "ND" ,]

#or

subset(dat, V2 != "ND")

Does it make sense to address the offending "ND" data before converting it into a zoo object? Does ND stand for "no data", i.e. should be interpreted as NA?

txt <- "03/15/2011 0.63 
03/16/2011 0.58 
03/17/2011 0.60 
03/18/2011 0.61 
03/21/2011 0.67 
03/22/2011 ND 
03/23/2011 0.69 
03/24/2011 0.72 
03/25/2011 0.79 
03/28/2011 0.81 
03/29/2011 0.81 
03/30/2011 0.80 
03/31/2011 0.80"

#If ND == NA
dat <- read.table(textConnection(txt), header = FALSE, na.strings = "ND") 

#if not
dat <- read.table(textConnection(txt), header = FALSE) 

dat[dat$V2 != "ND" ,]

#or

subset(dat, V2 != "ND")
蒗幽 2024-11-05 18:43:35

试试这个:

Lines <- "03/15/2011 0.63 
03/16/2011 0.58 
03/17/2011 0.60 
03/18/2011 0.61 
03/21/2011 0.67 
03/22/2011 ND 
03/23/2011 0.69 
03/24/2011 0.72 
03/25/2011 0.79 
03/28/2011 0.81 
03/29/2011 0.81 
03/30/2011 0.80 
03/31/2011 0.80"

library(zoo)

z <- read.zoo(textConnection(Lines), format = "%m/%d/%Y", na.strings = "ND")
zz <- na.omit(z)

plot(zz)

Try this:

Lines <- "03/15/2011 0.63 
03/16/2011 0.58 
03/17/2011 0.60 
03/18/2011 0.61 
03/21/2011 0.67 
03/22/2011 ND 
03/23/2011 0.69 
03/24/2011 0.72 
03/25/2011 0.79 
03/28/2011 0.81 
03/29/2011 0.81 
03/30/2011 0.80 
03/31/2011 0.80"

library(zoo)

z <- read.zoo(textConnection(Lines), format = "%m/%d/%Y", na.strings = "ND")
zz <- na.omit(z)

plot(zz)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文