尽管对象是data.frame

时间:2016-07-12 12:13:04

标签: r dplyr

我有一些数据的子集(如下所示)。现在由于某种原因,我不能以任何方式改变data.frame。

我收到错误消息:

  

错误:data_frames只能包含1d原子矢量和列表

这应该源于我没有返回一个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 个答案:

答案 0 :(得分: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]