-3

我有一个像这样打印的未知 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”分开,以便(最终)循环?

字符串(我的对象)

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"

require(plyr) 加载所需的包:plyr ldply(myRobject, identity)

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"

股票代码<-list(myRobject[[1]][1],myRobject[[2]][1])

股票代码

[[1]]
theTicker
"MSFT"

[[2]]
theTicker
"XXXX"

周期<-list(myRobject[[1]][2],myRobject[[2]][2])

period

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

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

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

4

3 回答 3

2

有一个plyr解决方案:

require(plyr)
ldply(myRobject, identity)

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

  • 很简单
  • plyrplyr非常适合很多任务,所以我认为养成尽可能寻找解决方案的习惯是有帮助的。
于 2013-08-12T04:05:44.040 回答
1

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

do.call(rbind,ll)
     theTicker thePeriodEnded
[1,] "MSFT"    "31-03-2013"  
[2,] "ORCL"    "31-03-2012"  
于 2013-08-12T02:42:41.840 回答
0
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) 
于 2013-08-12T02:52:15.493 回答