Unlist函数将数值转换为字符。R

Unlist函数将数值转换为字符。R,r,list,dataframe,R,List,Dataframe,代码正在抓取网站上的股票数据,并为每个股票返回一个1x18数据帧。我试图将数据帧转换为向量,而不将数字列转换为因子,这就是正在发生的情况。我还尝试将数据帧转换为矩阵,但数字列仍在转换为因子。总之,我希望将字符保留为字符,将数字保留为数字,所有这些都放在一个向量中。多谢各位 #get.dates is a function I created to scrape data = get.dates("AAPL") class(data) [1] "data.fram

代码正在抓取网站上的股票数据,并为每个股票返回一个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)))
tickers=c(“AAPL”、“PEP”、“KO”)
系统时间({
data=foreach(r=tickers,.packages=c(“jsonlite”,“dplyr”),.combine=rbind)%dopar%{get.dates(r)}
})
调用组合函数时出错:

你基本上必须在这里做你自己的列表展平,这是不可取的。当您最初获得json数据时,这样做更容易

下面的解决方案用户
purrr
,但如果愿意,您可以使用for循环或apply函数来实现。这里有两个主要观点:
1.将dataframe类型的列与dataframe中没有任何嵌套列的部分绑定在一起。在您的示例中,我们将3个单独的部分绑定在一起:1个删除了df_cols的原始数据帧,以及另外两个数据帧列。您可以使用
bind\u 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)

flatte_df_cols不清楚预期的输出是什么,因为您混合了多种类型,它将使用最小的公分母,即字符。如果需要数值结果,请先删除非数值组件,或者为其指定数值。也许你真的想改变你想做的事情,向量和矩阵只能有一种数据类型。这是他们定义的一部分。我编辑了原始帖子以提供更多的上下文谢谢尤金!平展json文件最初非常有效!!例如,fromJSON(…,flatte=T)。谢谢你的深刻回答
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