2016-07-12 37 views
1

我有一些數據的子集(如下所示)。現在由於某種原因,我無法以任何方式改變data.frame。儘管對象爲數據,但不能進行變異。

我得到的錯誤信息:

Error: data_frames can only contain 1d atomic vectors and lists

應從我沒有返回一個對象類,或者是一個listdata.frame本身,但是我甚至不能使突變顯然返回numeric值幹。

數據

> FValuation.head 
      conm firm.value   isin fyear   Industry Total.PMG Exchange Industry.Classification    List Short.Name Feb.Price Current.Price 
1  2E GROUP 15.2460627 SE0000680902 2015f Other service  0.62  STO  Consumer Services First North Premier   2E  12.75   12.95 
2  A-COM AB   NA SE0000592677 2015f Other service  0.62  <NA>     <NA>    <NA>  <NA>  NA   NA 
3  AAK AB 423.2503370 SE0001493776 2015f Other production  0.31  STO   Consumer Goods    LARGE  AAK 430.00  425.00 
4  AB SAGAX   NA SE0001629288 2015f  Real estate  0.56  STO    Financials     MID SAGA PREF  NA   NA 
5  ABELCO AB 0.3730399 SE0003617075 2015f Other production  0.31  STO     <NA>   AktieTorget  ABE  1.69   2.00 
6 ACADEMEDIA AB   NA SE0007897079 2015f Other service  0.62  <NA>     <NA>    <NA>  <NA>  NA   NA 

> str(FValuation.head) 
'data.frame': 6 obs. of 12 variables: 
$ conm     : chr "2E GROUP" "A-COM AB" "AAK AB" "AB SAGAX" ... 
$ firm.value    : num 15.246 NA 423.25 NA 0.373 ... 
$ isin     : chr "SE0000680902" "SE0000592677" "SE0001493776" "SE0001629288" ... 
$ fyear     : chr "2015f" "2015f" "2015f" "2015f" ... 
$ Industry    : Factor w/ 16 levels "Building and construction",..: 11 11 10 14 10 11 
$ Total.PMG    : num 0.62 0.62 0.31 0.56 0.31 0.62 
$ Exchange    : Factor w/ 5 levels "","CPH","HEL",..: 5 NA 5 5 5 NA 
$ Industry.Classification: Factor w/ 11 levels "","Basic Materials",..: 4 NA 3 5 NA NA 
$ List     : Factor w/ 7 levels ""," ","AktieTorget",..: 4 NA 5 6 3 NA 
$ Short.Name    : Factor w/ 1058 levels "","203","2E",..: 3 NA 6 805 9 NA 
$ Feb.Price    : num [1:6, 1] 12.75 NA 430 NA 1.69 ... 
$ Current.Price   : num [1:6, 1] 12.9 NA 425 NA 2 ... 

> FValuation.summary <- FValuation.head %>% mutate(Buy.Sell.Signal = derivedFactor(
+           "NA" = (is.na(firm.value) == TRUE | is.na(Feb.Price) == TRUE), 
+           "sell" = (firm.value < Feb.Price), 
+           "buy" = (firm.value > Feb.Price), 
+           .method = 'first')) 
Error: data_frames can only contain 1d atomic vectors and lists 

> FValuation.head %>% mutate(test = firm.value * 2) 
Error: data_frames can only contain 1d atomic vectors and lists 

出了什麼問題?如何解決這個問題?

> dput(droplevels(FValuation.head)) 
structure(list(conm = c("2E GROUP", "A-COM AB", "AAK AB", "AB SAGAX", 
"ABELCO AB", "ACADEMEDIA AB"), firm.value = c(15.2460627037116, 
NA, 423.25033702408, NA, 0.373039901083465, NA), isin = c("SE0000680902", 
"SE0000592677", "SE0001493776", "SE0001629288", "SE0003617075", 
"SE0007897079"), fyear = c("2015f", "2015f", "2015f", "2015f", 
"2015f", "2015f"), Industry = structure(c(2L, 2L, 1L, 3L, 1L, 
2L), .Label = c("Other production", "Other service", "Real estate" 
), class = "factor"), Total.PMG = c(0.62, 0.62, 0.31, 0.56, 0.31, 
0.62), Exchange = structure(c(1L, NA, 1L, 1L, 1L, NA), .Label = "STO", class = "factor"), 
    Industry.Classification = structure(c(2L, NA, 1L, 3L, NA, 
    NA), .Label = c("Consumer Goods", "Consumer Services", "Financials" 
    ), class = "factor"), List = structure(c(2L, NA, 3L, 4L, 
    1L, NA), .Label = c("AktieTorget", "First North Premier", 
    "LARGE", "MID"), class = "factor"), Short.Name = structure(c(1L, 
    NA, 2L, 4L, 3L, NA), .Label = c("2E", "AAK", "ABE", "SAGA PREF" 
    ), class = "factor"), Feb.Price = structure(c(12.75, NA, 
    430, NA, 1.69, NA), .Dim = c(6L, 1L)), Current.Price = structure(c(12.95, 
    NA, 425, NA, 2, NA), .Dim = c(6L, 1L))), .Names = c("conm", 
"firm.value", "isin", "fyear", "Industry", "Total.PMG", "Exchange", 
"Industry.Classification", "List", "Short.Name", "Feb.Price", 
"Current.Price"), row.names = c(NA, 6L), class = "data.frame") 
+1

mosaic :: derivedFactor()可能比使用dplyr時if語句的標準ifelse()替代方法更受歡迎。 請參閱:http://stackoverflow.com/questions/22337394/combine-mutate-with-conditional-values – uncool

回答

3

因爲你的數據幀的最後2列包含矩陣,即它有2個維度,請參閱:

class(FValuation.head$Feb.Price) 

如果你刪除(或轉換爲數值)最後2列,它應該工作:

FValuation.head[, 1:10] %>% 
      mutate(test = firm.value * 2) 

str(FValuation.head$Feb.Price)將顯示它有6行1列。

$ Feb.Price : num [1:6, 1] 
+1

您可以使用'select'或'mutate_at' – akrun

相關問題