【问题标题】:Can't mutate despite object as data.frame尽管对象为 data.frame,但不能变异
【发布时间】:2016-11-14 16:18:34
【问题描述】:

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

我收到错误消息:

错误:data_frames 只能包含一维原子向量和列表

这应该源于我没有返回 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")

【问题讨论】:

标签: r dplyr


【解决方案1】:

因为您的数据框的最后 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]

【讨论】:

  • 您可以使用selectmutate_at
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
相关资源
最近更新 更多