Problem with replacement dataframes

I'm working on a Twitter analysis. I'm trying to automate the extraction of timeline of the followers of a certain twitter account.

This is my code

library(tidyverse)
library(rtweet)

set.seed(1234)


filas <- nrow(twitter)
muestra <- sample(filas*0.30)

twitter_sample <- twitter[muestra,]

twitter_sample$Nick.del.Cliente <- as.character(twitter_sample$Nick.del.Cliente)

i <- 1

for (i in 1:length(muestra)){
  usuario <- paste('@', twitter_sample$Nick.del.Cliente, sep = '' )
  }

(usuario)

j <- 1

texto <-  data.frame(matrix(ncol = 283, nrow = 100))
tipo <- data.frame(matrix(ncol = 283, nrow = 100))
cantidad <- data.frame(matrix(ncol = 283, nrow = 100))



for (j in 1:283){
  total <- get_timeline(usuario[j], n =100, include_rts = F)
  texto[,j] <- total$text
  tipo[,j] <- total$source
  cantidad[,j] <- total$display_text_width
}

I choose a representative sample of the followers, then I added a "@" to every single ID user and create a list with the ID called "usuario" (usuario means user in Spanish).

Then I created 3 empty data frames, when colums are equal to users that I have and rows are equal to the numbers of tweets that I need.

In 'for' I need the next things:

  1. Access to the first user's timeline
  2. Take 'text' column and add it in to first column of 'texto' dataframe
  3. Take 'source' column and add it in to first column of 'tipo' dataframe
  4. Take 'display_text_width; and add it in to first column of 'cantidad' dataframe

Then repeat the same with the second user and add the same information in the second column to the dataframes, and then de third users, etc...

When I run the second 'for' I get this error.

Error in `[<-.data.frame`(`*tmp*`, , j, value = c("@Bellealouette Influencia? Quien es ella que no sabe que es influenza?",  : 
  replacement has 96 rows, data has 100.

If i change the dataframe size the error is the same, but with other numbers

Error in `[<-.data.frame`(`*tmp*`, , j, value = c("@Bellealouette Influencia? Quien es ella que no sabe que es influenza?",  : 
  replacement has 78 rows, data has 80

I think you have this error because your different users won't have the same number of total$text and total$source and you are trying here to assign those vectors into data.frame colums of fixed size. Columns must have the same number of elements.

You either need to work by list instead, or deal with your data to have the same number of elements in each vector before creating the data.frame.

Working with the tidyverse (dplyr,tidyr and purrr) can help you get your data in table form.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.