【问题标题】:Unlist Function Is Turning Numeric Values Into Characters. RUnlist 函数将数值转换为字符。 R
【发布时间】:2019-03-03 21:21:14
【问题描述】:

代码正在从网站上抓取股票数据,并为每只股票返回一个 1x18 的数据框。我正在尝试将数据框转换为向量而不将数字列转换为正在发生的因素。我还尝试尝试将数据框转换为矩阵,但数字列仍在转换为因子。总之,我想将字符作为字符和数字作为数字都保留在向量中。谢谢。

    #get.dates is a function I created to scrape

    data = get.dates("AAPL")
    class(data)
    [1] "data.frame"
    class(data$surprise)
    [1] "numeric"
    dput(data)
    structure(list(date = "2019-05-07T00:00:00", company = "Apple", 
        ticker = "AAPL", periodEnding = "Mar 2019", eps = "2.37", 
        reportedEPS = NA_character_, lastEps = "2.73", consensus = 4L, 
        bpConsensus = 4L, ratingsAndPT = structure(list(priceTarget = 177.34, 
            numBuys = 17L, numHolds = 18L, numSells = 0L), class = "data.frame", row.names = c(NA, 
        -1L)), bpRatingsAndPT = structure(list(priceTarget = 176.88, 
            numBuys = 14L, numHolds = 14L, numSells = 0L), class = "data.frame", row.names = c(NA, 
        -1L)), marketCap = 827573630900, sector = 18731L, stockId = 7624L, 
        stockTypeId = 1L, surprise = NA_real_, timeOfDay = 4L, isConfirmed = FALSE), class = "data.frame", row.names = c(NA, 
    -1L))
    data = unlist(data)
    class(data)
    [1] "character"

所以最终的输出是将每个输出rbind到一个单独的data.frame中。 我想我必须将每个 1x18 数据帧转换为 rbind 的向量,因为在尝试使用 foreach 包对列进行 rbind 时出现错误。

tickers = c("AAPL", "PEP", "KO")
system.time({
  data = foreach(r = tickers, .packages = c("jsonlite", "dplyr"), .combine = rbind) %dopar% {get.dates(r)}
})
error calling combine function:
<simpleError in `.rowNamesDF<-`(x, value = value): duplicate 'row.names' are not allowed>
   user  system elapsed 
   0.02    0.00    0.56 
Warning message:
non-unique value when setting 'row.names': ‘1’ 

print(data)
NULL
#I will do the same thing outside of the foreach loop to give some more context
data = lapply(tickers, get.dates)
do.call(rbind, data)
Error in `.rowNamesDF<-`(x, value = value) : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique value when setting 'row.names': ‘1’ 

dput(data)
list(structure(list(date = "2019-05-07T00:00:00", company = "Apple", 
    ticker = "AAPL", periodEnding = "Mar 2019", eps = "2.37", 
    reportedEPS = NA_character_, lastEps = "2.73", consensus = 4L, 
    bpConsensus = 4L, ratingsAndPT = structure(list(priceTarget = 177.34, 
        numBuys = 17L, numHolds = 18L, numSells = 0L), class = "data.frame", row.names = c(NA, 
    -1L)), bpRatingsAndPT = structure(list(priceTarget = 176.88, 
        numBuys = 14L, numHolds = 14L, numSells = 0L), class = "data.frame", row.names = c(NA, 
    -1L)), marketCap = 827573630900, sector = 18731L, stockId = 7624L, 
    stockTypeId = 1L, surprise = NA_real_, timeOfDay = 4L, isConfirmed = FALSE), class = "data.frame", row.names = c(NA, 
-1L)), structure(list(date = "2019-04-23T00:00:00", company = "Coca-Cola", 
    ticker = "KO", periodEnding = "Mar 2019", eps = "0.46", reportedEPS = NA_character_, 
    lastEps = "0.47", consensus = 4L, bpConsensus = 5L, ratingsAndPT = structure(list(
        priceTarget = 50.89, numBuys = 4L, numHolds = 5L, numSells = 0L), class = "data.frame", row.names = c(NA, 
    -1L)), bpRatingsAndPT = structure(list(priceTarget = 51.25, 
        numBuys = 3L, numHolds = 1L, numSells = 0L), class = "data.frame", row.names = c(NA, 
    -1L)), marketCap = 193681840000, sector = 18731L, stockId = 8359L, 
    stockTypeId = 1L, surprise = NA_real_, timeOfDay = 4L, isConfirmed = FALSE), class = "data.frame", row.names = c(NA, 
-1L)), structure(list(date = "2019-04-25T00:00:00", company = "PepsiCo", 
    ticker = "PEP", periodEnding = "Mar 2019", eps = "0.92", 
    reportedEPS = NA_character_, lastEps = "0.96", consensus = 4L, 
    bpConsensus = 4L, ratingsAndPT = structure(list(priceTarget = 123.67, 
        numBuys = 4L, numHolds = 3L, numSells = 0L), class = "data.frame", row.names = c(NA, 
    -1L)), bpRatingsAndPT = structure(list(priceTarget = 126, 
        numBuys = 1L, numHolds = 1L, numSells = 0L), class = "data.frame", row.names = c(NA, 
    -1L)), marketCap = 163697620000, sector = 18731L, stockId = 10962L, 
    stockTypeId = 1L, surprise = NA_real_, timeOfDay = 4L, isConfirmed = FALSE), class = "data.frame", row.names = c(NA, 
-1L)))

这是我希望输出的样子。谢谢!!

【问题讨论】:

  • 不清楚预期的输出是什么
  • 由于您有多种类型,它将使用字符的最低公分母。如果您想要一个数字结果,请先删除非数字组件,否则为它们分配数值。也许你真的想改变你想做的这一切。
  • 向量和矩阵只能有一种数据类型。这是它们定义的一部分。
  • 我已经编辑了原始帖子以提供更多上下文

标签: r list dataframe


【解决方案1】:

您基本上必须在此处进行自己的列表展平,这是不可取的。当您最初获取 json 数据时,这样做会更容易。 https://rdrr.io/cran/jsonlite/man/flatten.html

以下解决方案用户 purrr 但如果您愿意,您可以使用 for 循环或应用函数来完成。这里有两个主要想法:
1. 将数据框类型的列与没有任何嵌套列的数据框部分绑定在一起。在您的示例中,我们将 3 个单独的部分绑定在一起:1 个删除了 df_cols 的原始数据帧,以及另外两个数据帧列。您可以使用bind_cols 执行此操作。它有助于添加原始列名以避免重复。
2. 用rbind之类的把所有的行折叠起来。

flatten_df_cols <- function(df) {
  df_cols <- map_lgl(df, is.data.frame)
  imap_dfc(df[, df_cols], ~setNames(.x, paste0(.y, ".", names(.x)))) %>% 
    bind_cols(list(df[, !df_cols]), .)
}

map_dfr(data, flatten_df_cols)
Observations: 3
Variables: 24
$ date                       <chr> "2019-05-07T00:00:00", "2019-04...
$ company                    <chr> "Apple", "Coca-Cola", "PepsiCo"
$ ticker                     <chr> "AAPL", "KO", "PEP"
$ periodEnding               <chr> "Mar 2019", "Mar 2019", "Mar 2019"
$ eps                        <chr> "2.37", "0.46", "0.92"
$ reportedEPS                <chr> NA, NA, NA
$ lastEps                    <chr> "2.73", "0.47", "0.96"
$ consensus                  <int> 4, 4, 4
$ bpConsensus                <int> 4, 5, 4
$ marketCap                  <dbl> 827573630900, 193681840000, 163...
$ sector                     <int> 18731, 18731, 18731
$ stockId                    <int> 7624, 8359, 10962
$ stockTypeId                <int> 1, 1, 1
$ surprise                   <dbl> NA, NA, NA
$ timeOfDay                  <int> 4, 4, 4
$ isConfirmed                <lgl> FALSE, FALSE, FALSE
$ ratingsAndPT.priceTarget   <dbl> 177.34, 50.89, 123.67
$ ratingsAndPT.numBuys       <int> 17, 4, 4
$ ratingsAndPT.numHolds      <int> 18, 5, 3
$ ratingsAndPT.numSells      <int> 0, 0, 0
$ bpRatingsAndPT.priceTarget <dbl> 176.88, 51.25, 126.00
$ bpRatingsAndPT.numBuys     <int> 14, 3, 1
$ bpRatingsAndPT.numHolds    <int> 14, 1, 1
$ bpRatingsAndPT.numSells    <int> 0, 0, 0

【讨论】:

  • 谢谢尤金!最初将 json 文件展平,效果很好!! IE。 fromJSON(..., flatten = T)。感谢您的深入回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
相关资源
最近更新 更多