2016-06-11 6 views
-1

2つの列を持つdata.frameを作成します。ツイートのIDを持つ第一、及び第二の列の情報はつぶやき返信であるかどうかに依存する、または私は2つが必要BUEdata.frame with twitter data

id_str | x$retweeted_status$id_str or x$in_reply_to_status_id_str 

私は3つの列を有するデータフレームを作ることができるリツイート。

マイコード:

ids <- sapply(tweets.list, function(x) x$id_str) 
    ret_ids <- sapply(tweets.list, function(x) if(is.null(x$retweeted_status)) NA else x$retweeted_status$id_str) 
    rep_ids <- sapply(tweets.list, function(x) if(is.null(x$in_reply_to_status_id_str)) NA else x$in_reply_to_status_id_str) 
    isnt.null <- function(x)!is.null(x) 
r_ids <- sapply(tweets.list, function(x) if(is.null(x$retweeted_status)) x$in_reply_to_status_id_str else x$retweeted_status$id_str) 
data.frame(ids,r_ids) 

出力:

Error in data.frame("733222936912351232", NULL, "733220677721968641", : 
    arguments imply differing number of rows: 1, 0 

データ:

ids|ret_ids|rep_ids 
1|40|NA 
2|32|NA 
3|NA|555 
4|NA|444 

望ましい結果:

ids|r 
1|40 
2|32 
3|555 
4|444 
+0

一つの方法だ(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-exampleそれ[再現性の例]にするためにいくつかのデータを追加してください。/5963610#5963610) – alistaire

+0

完了。ありがとうございました。 –

+0

実際にコードを実行できるデータはまだありません。 3つの列を2つの '' data.frame(ids = df [、1]、r = rowSums(df [、-1]、na.rm = T)) 'にmungeしたいだけなら。 – alistaire

答えて

0

ここ

df <- read.table(header=T, sep="|", text="ids|ret_ids|rep_ids 
1|40|NA 
2|32|NA 
3|NA|555 
4|NA|444") 

setNames(as.data.frame(t(apply(df, 1, na.omit))), c("ids", "r")) 
# ids r 
# 1 1 40 
# 2 2 32 
# 3 3 555 
# 4 4 444