-1

私は data.frame を持っていますが、各行をキーとして列名と値をそれぞれの行のフレーム値としてリストに変換したいと考えています。

f <- function(row) { mongo.bson.from.list(row to list) }
apply(df, 1, f)

データフレームは次のようになります

> h
  id      stamp stmt1 stmt2 stmt3 stmt4 stmt5 stmt6 stmt7 stmt8
1  1 1398288482   "0"   "1"   "0"   "1"   "1"   "1"   "0"   "1"
2  2 1398288765   "1"   "0"   "0"   "0"   "1"   "1"   "0"   "0"
3  3 1398288804   "1"   "1"   "1"   "1"   "1"   "1"   "1"   "1"

私が欲しいのは、データフレームの各行を自動的に変換する方法です

list(id=1, stamp=1398288482, stmt1="0", stmt2="1", ...)
4

1 に答える 1

1

@akrun の提案のバリエーションを見るのは興味深いです。

lapply(split(h, 1:nrow(h)), as.list)  # does deliver the requested object
lapply(split(h, 1:nrow(h)), list)     # a three element list with named atomic vectors
lapply(split(h, 1:nrow(h)), c)       # same as first
lapply(split(h, 1:nrow(h)), structure) #looks the same as first, but are actually data.frames

「正規化された」形式が必要な場合は、reshape( ..., direction="long")-approach が役立つ場合があります。

> reshape(h, varying=3:10, direction="long", sep="")
    id      stamp time stmt
1.1  1 1398288482    1    0
2.1  2 1398288765    1    1
3.1  3 1398288804    1    1
1.2  1 1398288482    2    1
2.2  2 1398288765    2    0
3.2  3 1398288804    2    1
1.3  1 1398288482    3    0
2.3  2 1398288765    3    0
3.3  3 1398288804    3    1
1.4  1 1398288482    4    1
2.4  2 1398288765    4    0
3.4  3 1398288804    4    1
1.5  1 1398288482    5    1
2.5  2 1398288765    5    1
3.5  3 1398288804    5    1
1.6  1 1398288482    6    1
2.6  2 1398288765    6    1
3.6  3 1398288804    6    1
1.7  1 1398288482    7    0
2.7  2 1398288765    7    0
3.7  3 1398288804    7    1
1.8  1 1398288482    8    1
2.8  2 1398288765    8    0
3.8  3 1398288804    8    1
于 2014-10-25T19:07:52.793 に答える