Removing NA values from a data frame or time-series object in R

余生长醉 提交于 2019-12-25 03:16:38

问题


I read in some data via:

it.data <- read.csv("inputData/rstar.data.it.csv", header = T, sep = ",") then the second and fourth columns are inflation resp. interest:

inflation.it <- it.data[2]

and

interest.it <- it.data[4].

However, the trouble starts when I am trying to reform the data into a time-series object, because there are leading and trailing NA values in the columns. I have tried na.omit(), it.data[complete.cases(it.data),], na.contiguous, without luck. What happens now is that when I try to transform the data into a TS object,

inflation.ts.it <- ts(inflation.it, frequency = 4, interest.start)

I get very strange values which do not match with the original data.

Thanks.

PS. The data (I did not post everything, but just to get an idea):

     gdp.log       inflation  inflation.expectations     interest
1          .    2.4361259655                       .            .
2          .    2.9997029997                       .            .
3          .    1.5169194865                       .            .
4          .    1.5059368664        2.11467132957868            .
5          .    2.0591647331        2.02043102148892            .
6          .    1.9896193771        1.76791011585382            .
7          .    2.6436781609        2.04959978443843            .
8          .    3.3951497860        2.52190301432020            .
9          .    4.5467462347        3.14379838970698            .
10         .    5.0890585241        3.91865817645959            .
11         .    5.7110862262        4.68551019278066            .
12         .    7.7262693156        5.76829007519398            .
13         .    7.5292198967        6.51390849069030            .
14         .    6.9679849340        6.98364009316870            .
15         .    7.6006355932        7.45602743492283            .
16         .    5.6352459016        6.93327158141434            .
17         .    5.4853387259        6.42230128873304            .
18         .    6.6649899396        6.34655254012084            .
19         .    5.8577405857        5.91082878825926            .
20         .    5.5528612997        5.89023263777669            .
21         .    4.9125329499        5.74703119375926            .
22         .    4.2442820089        5.14185421108985            .

回答1:


Assuming the dots are in the original CSV, you can fix it by specifying "." as na.string upon read-in.

read.csv(text=
"gdp.log,inflation,inflation.expectations,interest
.,2.4361259655377,.,.
.,2.99970299970301,.,.
.,1.5169194865811,.,.
.,1.50593686649291,2.11467132957868,.
.,2.05916473317866,2.02043102148892,.
.,1.9896193771626,1.76791011585382,.
.,2.64367816091953,2.04959978443843,.",
header=TRUE, na.string=c(".", "NA"))

na.string can be a vector of character strings, in case several codes are used for missing values.



来源:https://stackoverflow.com/questions/50525787/removing-na-values-from-a-data-frame-or-time-series-object-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!