【问题标题】:R language iterate over R objectR语言迭代R对象
【发布时间】:2013-08-12 02:35:48
【问题描述】:

我有一个像这样打印的未知 R 对象。如何迭代这些值并打印它们?

    print(myRobject)

[[1]]
                  theTicker                  thePeriodEnded
                     "MSFT"                    "31-03-2013"
              theRevenueRaw                 theNetIncomeRaw
                    "20489"                          "6055"
theEarningsPerShareBasicRaw    theWeightedAveSharesBasicRaw
                     "0.72"                          "8364"

theCashDivDeclPerCommonShareRaw theNetCashFromOperationsRaw "0.23" "9666"

[[2]]
                  theTicker                  thePeriodEnded
                     "XXXX"                    "31-03-2013"
              theRevenueRaw                 theNetIncomeRaw
                    "20489"                          "6055"
theEarningsPerShareBasicRaw    theWeightedAveSharesBasicRaw
                     "0.72"                          "8364"

theCashDivDeclPerCommonShareRaw theNetCashFromOperationsRaw "0.23" "9666"

Dean 和 Metrics,这是一些结果。 如何将“theTicker”与“MSFT”分开,以便(最终)循环?

str(myRobject)

List of 1
$ : Named chr [1:8] "MSFT" "31-03-2013" "20489" "6055" ...
..- attr(*, "names")= chr [1:8] "theTicker" "thePeriodEnded" "theRevenueRaw" "theNetIncomeRaw" ...

我的对象[[1]]

WORKS - returns just list item 1

myRobject[[1]]["theTicker"]

theTicker
"MSFT"

str(myRobject[[1]]["theTicker"])

Named chr "MSFT"
- attr(*, "names")= chr "theTicker"

需要(plyr) 加载所需的包:plyr ldply(myRobject, 身份)

theTicker thePeriodEnded theRevenueRaw theNetIncomeRaw
1      MSFT     31-03-2013         20489            6055
2      XXXX     31-03-2013         20489            6055
theEarningsPerShareBasicRaw theWeightedAveSharesBasicRaw
1                        0.72                         8364
2                        0.72                         8364
theCashDivDeclPerCommonShareRaw theNetCashFromOperationsRaw
1                            0.23                        9666
2                            0.23                        9666

出[[1]]["theTicker"][1]

theTicker
"MSFT"

出[[1]]["theTicker"][2]

<NA>
NA

出[[1]]["theTicker"]["1"]

<NA>
NA

出[[1]]["theTicker"][["1"]]

ERROR

出[[1]]["theTicker"]$1

ERROR

出[[1]]["theTicker"][1,1]

Error in out[[1]]["theTicker"][1, 1] : incorrect number of dimensions

出[[1]]["theTicker"][1]

theTicker
"MSFT"

出[[1]]["theTicker"][1][1]

theTicker
"MSFT"

出[[1]]["theTicker"][1][2]

<NA>
 NA

出[[1]]["theTicker"][1][1][1]

theTicker
"MSFT"

do.call(rbind,myRobject)

theTicker thePeriodEnded theRevenueRaw theNetIncomeRaw
[1,] "MSFT"    "31-03-2013"   "20489"       "6055"
[2,] "XXXX"    "31-03-2013"   "20489"       "6055"
theEarningsPerShareBasicRaw theWeightedAveSharesBasicRaw
[1,] "0.72"                      "8364"
[2,] "0.72"                      "8364"
theCashDivDeclPerCommonShareRaw theNetCashFromOperationsRaw
[1,] "0.23"                          "9666"
[2,] "0.23"                          "9666"

ticker

代码

[[1]]
theTicker
"MSFT"

[[2]]
theTicker
"XXXX"

期间

period

[[1]]
thePeriodEnded
"31-03-2013"

[[2]]
thePeriodEnded
"31-03-2013"

再次,我如何将“theTicker”与“MSFT”分开,这样我可以(最终)循环?

【问题讨论】:

  • 我不明白 - 你似乎可以print 它就好了。结果你到底想要什么?
  • str 在您有一个未知对象并想弄清楚它是什么时很有用

标签: r loops


【解决方案1】:

有一个plyr 解决方案:

require(plyr)
ldply(myRobject, identity)

这会在每个列表元素上调用“身份”并将结果作为数据框返回。这个解决方案既不是特别快,也不是特别健壮:如果列表元素具有不同的列数,它将失败。但是,我喜欢它,因为

  • 很简单
  • plyr 非常适合执行大量任务,因此我认为养成尽可能寻找 plyr 解决方案的习惯会有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用lapply 或其他xxapply 函数来遍历列表。但是这里将其转换为矩阵更简单:

    do.call(rbind,ll)
         theTicker thePeriodEnded
    [1,] "MSFT"    "31-03-2013"  
    [2,] "ORCL"    "31-03-2012"  
    

    【讨论】:

    • 同样,require(plyr); ldply(myRobject, identity)
    • @DrewSteen 我认为这不应该是这个答案下的评论......也许你可以将它作为另一个答案发布。
    【解决方案3】:
    ticker<-list(myRobject[[1]][1],myRobject[[2]][1])
    period<-list((myRobject[[1]][2],myRobject[[2]][2])
    

    现在您可以在 Map 函数中使用代码和句点作为输入

    Map(function(x,y) your function, ticker period) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2014-04-30
      • 1970-01-01
      相关资源
      最近更新 更多