na.omit是我的data.frame

时间:2018-03-10 19:20:19

标签: r na

我有一个包含大量数据的Data.frame。 760 obs / 465 var。 我里面有几个NA值。 当我使用na.omit删除带有NA的行时,它会杀死我的整个data.frame。 所以我只有var。左侧和所有对象都被删除。 任何想法如何解决这个问题?

>     # Install Section
> # to use R and all it's packges you have to run the lines below first. 
> 
> pckgs<-c("Quandl","Sweep","tidyverse","tidyquant","ggplot","forcats","stringr","PerformanceAnalytics","xts","BatchGetSymbols","RCurl","XML","quantmod","eventstudies","ggplot2","stringr","glue","tidytext","xlsx","zoo")
> install.packages(pckgs,dependencies = TRUE) install.packages("rJava")
> library(Quandl) library(tidyverse) library(ggplot2) library(tidyquant)
> library(timetk) library(forcats) library(stringr) library(gganimate)
> library(plyr) library(stringr) library(gridExtra)
> library(BatchGetSymbols) library(RCurl) library(XML) library(xts)
> library(quantmode) library(eventstudies) library(quantmod)
> library(xlsx) library(rJava) library(zoo) library(devtools)
> devtools::install_github("EventStudyTools/api-wrapper.r")
> library(openxlsx)
> #-------------------------------------------------------------------------
> #to stop a running script {   if (TRUE) {stop("The value is TRUE, so the script must end here")}
>      print("Script did NOT end!") }
> 
> # Clear Enviroment
> 
> rm(list=ls())
> 
> #-------------------------------------------------------------------------
> 
> #set the directory where you r programm will be saved.
> #Windows option dir <- setwd(choose.dir()) #when you run the code for the first time.  dir <- setwd("C:/Users/JonasFlammiger/Dropbox/FH
> Kiel/7. Semester/Thesis/R Prog") dir
> # sets a Date fitst.date <- as.Date("2007-01-01") last.date <- as.Date("2018-01-23")
> 
> # generates a XTS-File wich will download the adjusted stockprices on a Daily base.  url <-
> "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies" tables <-
> getURL(url)
> 
> tables <- readHTMLTable(tables, stringsAsFactors = F) sp500symbols <-
> tables[[1]][,1:2] head(sp500symbols) rm(list="tables", "url")
> quantmod_list <-c(sp500symbols$`Ticker symbol`)
> 
> dataEnv <- new.env()
> 
> for(company in quantmod_list){   try(getSymbols(company, auto.assign =
> TRUE, src="yahoo", from="2007-01-01", to="2010-01-10",return.class =
> 'zoo', env=dataEnv))   print(company) } plist <- eapply(dataEnv, Ad)
> ad.Stocks <- do.call(merge, plist)  
> 
> # generates the dataset "Stockreturns" <- StockPriceReturns Stockreturns <- data.frame(c(diff(log(ad.Stocks))))  #calc. the
> stockreturns write.xlsx(Stockreturns, file = "Stockreturns.xlsx",
> sheetName = "Stockreturns") #export the stockreturns into a xlsx file
> Stockreturns <- na.omit(Stockreturns)
> # generates the dataset "Marketreturns" <- OtherReturns getSymbols("^GSPC", auto.asssign= TRUE, src="yahoo",from="2007-01-01",
> to="2010-01-10",return.class = 'zoo')  #download SP500 historical data
> GSPC <- as.xts(GSPC)                                                  
> #generates a xts.file Marketreturns <- data.frame(c(diff(log(GSPC$GSPC.Adjusted))))#cal. the marketreturn
> Marketreturns <- na.omit(Marketreturns) write.xlsx(Marketreturns, file
> = "Marketreturns.xlsx", sheetName = "Marketreturns")

0 个答案:

没有答案