###前提・実現したいこと
Rで、ライブラリdata.tableを使い、以下のような表を作りました。
R
1> DT = data.table(id=1:2, comment=list( c("mary", "hello"), 2 c("bob", "world") )) 3> DT 4 id comment 51: 1 mary, hello 62: 2 bob, world
この表のid=1のところに、新しく"hoge"という要素を追加したいのですが、なかなかうまくいきません。(以下のようにしたいです)
R
1> DT 2 id comment 31: 1 mary, hello, hoge 42: 2 bob, world
###試したこと
data.tableはカラム自体がリストなのでlistを重ねなければいけないというのは調べたのですが、実践すると、以下のように微妙なリストになってしまいます。
R
1> DT[id==1, comment:=list(list(list(comment,list("hoge"))))] 2 3> DT 4 id comment 51: 1 list(list(list("mary", "hello")), list("hoge")) 62: 2 bob, world
二重リストに入っているmary・helloとhogeが並んでいるようにみえるので、listを減らすと、要素が余計だと怒られてしまい、hogeが切り捨てられてしまうようです。
R
1> DT[id==1, comment:=list(list(comment,list("hoge")))] 2Warning message: 3In `[.data.table`(DT, id == 1, `:=`(comment, list(list(comment, : 4 Supplied 2 items to be assigned to 1 items of column 'comment' (1 unused)
良い方法をご存じの方、ご教授よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー