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

自闭症网瘾萝莉.ら 提交于 2020-03-26 03:52:11

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/59774271/class-and-type-of-object-is-different-in-r-how-should-i-make-it-consistent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!