Can I create a tibble with 1 row and 11 columns in R from a data frame with 0 rows and 0 columns?

J001

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.

enter image description here

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!

Ronak Shah

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.

edited at
0

Comments

0 comments
Login to comment

Related

Remove 0 columns from a data frame in R

R/Dplyr: Data Frame - Filling in NA/NULL values with 0, Columns for each row to be filled in is based on value from each rows specific value

R matrix - how to convert data from data frame columns into 1/0 matrix

How can I remove rows and columns with unwanted 0's r?

How can I move data from multiple columns and multiple rows to 1 single row in excel

How can I create a new wide data frame with rows based on all combos of values in two columns?

How can I create a new series by using specific rows and columns of a pandas data frame?

Delete a row or consecutive rows (from 1 to 5) in which there is 0 as a value of one of the two columns (or both)

How to create a list of data.frame with matched rows and columns in R

Panda Data frame : How to merge columns of 1 and 0 to a new column

compare two columns in data frame, then produce 1 or 0 if they are equal or not

How can I create an array in php with 20 rows and 20 columns and their values is 0?

R: given a matrix of 0 and 1s create matrix showing repetition between columns in rows

how can I create a new data frame using exact rows from the old data frame in R Studio?

How can I use if else to change values in some rows and columns in my data frame in R?

How can I take repeating rows and transform the data frame to add columns of unique identifiers in R?

How can I compare two rows in R data frame by different columns and perform an operation on them?

Is there a way I can collapse the data frame into ONE row with associated columns

create an empty array with 0 row and 4096 columns this number from feature

R: aggregation (median) of data.frame rows from >2 columns

In R, how do I match values from selected rows in one data frame with selected columns in another?

R Create multiple rows from 1 row based on presence of values in certain columns

Mutating chosen columns in the data.frame or tibble with dplyr package R

R: data.table count rows on specific columns > 0

Create columns from unique element from data.frame in R

R: tibble move columns to row & name columns

How can I create a new data frame based on the existing columns?

How can I assign values (-1, 0, 1) to the difference between consecutive rows in a MySQL table having 20 columns and 20 rows

Q: Python (pandas or other) - I need to "flatten" a data file from many rows, few columns to 1 row many columns