-1

I want to make a data.frame with two columns. The first with the Id of tweets, and the second column the information depends on whether the tweet is a reply or retweet

id_str | x$retweeted_status$id_str or x$in_reply_to_status_id_str

I can make a dataframe with three columns bue I need two.

My code:

    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)

The output:

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

Data:

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

Result desired:

ids|r
1|40
2|32
3|555
4|444
alistaire
  • 42,459
  • 4
  • 77
  • 117
ANOUK_prog
  • 27
  • 6
  • Please add some data to make it [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – alistaire Jun 12 '16 at 02:01
  • You still don't have any data that actually lets the code run. If you just want to munge the three columns into two, `data.frame(ids = df[,1], r = rowSums(df[,-1], na.rm = T))`. – alistaire Jun 12 '16 at 07:17

1 Answers1

0

Here's one way

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
lukeA
  • 53,097
  • 5
  • 97
  • 100