I have imported some Twitter data which gives me a list with tibbles for every user. Each tibble has 11 columns and various number of rows depending on how many lists a Twitter user has. If a Twitter user has no lists, it is listed as a data frame with 0 rows and 0 columns (see [3] in the picture). I don't want to delete such entries but keep them as a user with no lists.
Hence, I'm thinking whether I can create a tibble with 11 columns and 1 row where each cell contains a "99".
How do I change a data frame within a list to a tibble?
Thanks a lot for your help!
You can try :
#get index of dataframes that has 0 columns
inds <- lengths(list_data_outlier) == 0
#get column names from other dataframe which is not empty
cols <- names(list_data_outlier[[which.max(!inds)]])
#create an empty dataframe with data as 99 and 1 row
empty_df <- data.frame(matrix(99, nrow = 1, ncol = length(cols),
dimnames = list(NULL, cols)))
#replace the dataframes with 0 columns with empty_df
list_data_outlier[inds] <- replicate(sum(inds), empty_df, simplify = FALSE)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments