2017-04-22 4 views
0

안녕하세요 여러분, 제가 전에 알고있는 모든 게시물을 본 적이 있습니다. 그러나 어떤 이유로 든 제가 시도한 조언이 전혀 효과가 없었습니다. 본질적으로 내가하려는 것은 dd/mm/yyyy 형식의 "Production.Period.End.Date"라는 변수에서 날짜를 가져 와서이 날짜의 각 부분을 분석 할 개별 객체로 변환하는 것입니다. 내가 이것을하는 이유는 "Period_kWh_Production"이라고 표시된 연간 평균 킬로와트 생산량을 취하고 그 초과 근무 시간의 변화를 추적하기 위해서입니다. 그 코드가 내가 도움이된다면 아래에 두었습니다.문제가 올해 별도의 개체로 변하기 시작했습니다

setwd ("C : \ 사용자 \ fredd \ 드롭 박스 \ Grad_Life \ Spring_2017 \ AFM \ Final_Paper \")

KWTProd.df = read.csv("Merge1//Kwht_Production_07-15.csv", header=T) 

##Did this to verify "Production.Period.End.Date" 

names(KWTProd.df) 

## 
names(KWTProd.df) 
[1] "Application.Number"      
[2] "Program.Administrator"     
[3] "Program"         
[4] "Total.Cost"        
[5] "System.Owner.Sector"      
[6] "Host.Customer.Sector"     
[7] "Host.Customer.Physical.Address.City"  
[8] "Host.Customer.Physical.Address.County" 
[9] "Host.Customer.Physical.Address.Zip.Code" 
[10] "PBI.Payment.."       
[11] "Production.Period.End.Date"    
[12] "Period_kWh_Production" <-IT EXISTS ## 
## 

##Did this to plot changes of Period_kWh_Production over time## 

plot(Period_kWh_Production ~ Production.Period.End.Date, data = KWTProd.df) 

##Tried to do this to aggregate data in average## 

aggregate(Period_kWh_Production~Production.Period.End.Date,KWTProd.df,mean) 

##Still too noisy and can't find the mean by year :C## 

as.date(Production.Period.End.Date, data = KWTProd.df) 

##Says "Production.Period.End.Date" Not found BUT IT EXISTS## 

##Tried this to group and summarise by year but it says: Error in  UseMethod("mutate_") : 
no applicable method for 'mutate_' applied to an object of class "function"   ## 

summary <- df %>% 
    mutate(dates = dmy(Production.Period.End.Date), 
     year = year(Production.Period.End.Date)) %>% 
    group_by(year) %>% 
    summarise(mean = mean(x, na.rm = TRUE), 
      sd = sd(x, na.rm = TRUE)) 

##Trying this but have no clue how I am supposed to use this## 

regexpr("<dd>") 
+0

코드에 대해 잘 모르겠지만, 정규식은 '\ d 개입니다 {2}/\ D {2}/\ D {4}' – sln

답변

0

이 코드는 dplyr와 lubridate 패키지에 의존해야한다. 샘플 데이터를 제공하지 않았습니다. 그래서 이것은 테스트되지 않았습니다.

library(lubridate) 
library(dplyr) 

summary <- df %>% 
    mutate(end_date = dmy(Production.Period.End.Date), 
     production_year = year(end_date)) %>% 
    group_by(production_year) %>% 
    summarise(mean_kwH = mean(Period_kWh_Production, na.rm = TRUE), 
      sd_kwH = sd(Period_kWh_Production, na.rm = TRUE)) 
+0

는 그 시도했지만 어떤 이유로 나는가 계속 '에 대한 > 적용 가능한 방법 "(mean_kwH = 평균 (Period_kWh_Production, na.rm = TRUE), 이 sd_kwH = SD (Period_kWh_Production), na.rm = TRUE))를 요약 없습니다"오류 : 오류 :에') '예기치 않은 mutate_ '가 "function"클래스의 객체에 적용됨 –

+0

질문에 데이터를 추가하면 도움이됩니다. 일반적으로'dput' 함수를 사용하여 결과를 붙여 넣습니다. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example을 검토해 보시기 바랍니다. 나는 여분을 삭제 편집) – epi99

+0

미안하지만이게 더 힘들어하지만 dput은 큰 데이터 세트이기 때문에 숫자로 콘솔을 폭발시키는 것처럼 보입니다. 나는 이것이 어쨌든 도움이 될지 모르지만 당신이 보낸 링크의 주석을 기반으로 나는 붙여 넣기 빈을 사용하여 보여지는 결과의 수를 줄이기 위해 여전히 이것을 가지고있다 : –