1

I have a subset of some data (as shown below). Now for some reason I can't mutate the data.frame in any way.

I get the error message:

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

Which should stem from me not returning an object class that is either a list or data.frame itself, however I can't even make mutations that clearly returns a numeric value.

Data

> 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

What could be wrong? And how can this be resolved?

> 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")
zx8754
  • 52,746
  • 12
  • 114
  • 209
uncool
  • 2,613
  • 7
  • 26
  • 55
  • 1
    mosaic::derivedFactor() may be preferred to the standard ifelse() alternative for if statements when using dplyr. See: http://stackoverflow.com/questions/22337394/combine-mutate-with-conditional-values – uncool Jul 12 '16 at 12:17

1 Answers1

3

Because last 2 columns of your dataframe contain matrix, i.e. it has 2 dimensions, see:

class(FValuation.head$Feb.Price)

If you drop (or convert to numeric) last 2 columns, it should work:

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

str(FValuation.head$Feb.Price) will show that it has 6 rows and 1 column.

$ Feb.Price : num [1:6, 1]
akrun
  • 874,273
  • 37
  • 540
  • 662
zx8754
  • 52,746
  • 12
  • 114
  • 209