Shreya Agarwal
Shreya Agarwal

Reputation: 716

Class and type of object is different in R. How should I make it consistent?

I downloaded some tweets using 'rtweet' library. Its search_tweets() function creates a list (type) object, while its class is "tbl_df" "tbl" "data.frame". To further work on it, I need to convert this search_tweets() output into a dataframe.

comments <- search_tweets(
queryString, include_rts = FALSE,
n = 18000, type = "recent",
retryonratelimit = FALSE)

typeof(comments)

list

class(comments)

"tbl_df" "tbl" "data.frame"

I tried to convert list into dataframe by using as.data.frame(), that didn't change the type, I also tried wrapping it into as.dataframe(matrix(unlist(comments))), that didn't change the type as well

commentData <- data.frame(comments[,1]) 
    for (column in c(2:ncol(comments))){
        commentData <- cbind(commentData, comments[,column])
    }
type(comments)

output : list

comments <- as.data.frame(comments)

output : list

Both these codes didn't change the type, but the class. How should I change the type? As, I'd like to store these tweets into a dataframe and consequently write them as csv (write_csv).

As I write the 'comments' to csv, it throws an error.

write_csv(comments, "comments.csv", append =  TRUE) 

Error: Error in stream_delim_(df, path, ..., bom = bom, quote_escape = quote_escape) : Don't know how to handle vector of type list.

dput(comments)

dput(comments) structure(list(user_id = c("1213537010930970624", "770697053538091008", "39194086", "887369171603931137", "924786826870587392", "110154561", "110154561", "1110623370389782528", "1201410499788689408", "1208038347735805953", "15608380", "54892886", "389914405", "432597210", "1196039261125918720" ), status_id = c("1217424480366026753", "1217197024405143552", "1217057752918392832", "1217022975108616193", "1217002616757997568", "1216987196714094592", "1216986705170923520", "1216978052472688640", "1216947780129710080", "1216943924796739585", "1216925375789330432", "1216925016605880320", "1216924608944734208", "1216921598294249472", "1214991714688987136"), created_at = structure(c(1579091589, 1579037359, 1579004154, 1578995863, 1578991009, 1578987332, 1578987215, 1578985152, 1578977935, 1578977016, 1578972593, 1578972507, 1578972410, 1578971693, 1578511572), class = c("POSIXct", "POSIXt"), tzone = "UTC"), screen_name = c("SufferMario", "_Mohammadtausif", "avi_rules16", "Deb05810220", "SriPappumaharaj", "Poison435", "Poison435", "RajeshK38457619", "KK77979342", "beingskysharma", "tetisheri", "sohinichat", "nehadixit123", "panwarsudhir1", "NisarMewati1" ),

desired output in csv

screengrab of csv format

Upvotes: 0

Views: 282

Answers (1)

MrFlick
MrFlick

Reputation: 206243

You don't need to do anything. comments is already a data.frame. It just happens to be a special type of data.frame known as a tibble. But you can use them interchangeably. What do you want to do with comments that you currently cannot? It already should do anything a data.frame can do.

The output from typeof() is rarely helpful as it only shows you how the object is stored, not what it is. Use class() to understand how an object behaves. Nearly all "complex" objects in R are stored as lists.

Upvotes: 1

Related Questions