r tremeaud
r tremeaud

Reputation: 161

Convert Classes ‘tbl_df’, ‘tbl’ and 'data.frame into dataframe with R

I got this data:

> str(gaDataExt)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   2 obs. of  5 variables:
 $ date          : POSIXct, format: "2016-05-24" "2016-05-31"
 $ deviceCategory: chr  "desktop" "desktop"
  $ users         : int  1 2
  $ sessions      : int  1 2
  $ pageviews     : int  11 85
  - attr(*, "profileInfo")=List of 6
   ..$ profileId            : chr "25439551"
   ..$ accountId            : chr "12543305"
   ..$ webPropertyId        : chr "UA-12543305-1"
   ..$ internalWebPropertyId: chr "26790206"
   ..$ profileName          : chr "www.ciao.ch"
   ..$ tableId              : chr "ga:25439551"
  - attr(*, "query")=List of 8
   ..$ start.date : chr "30daysAgo"
   ..$ end.date   : chr "yesterday"
   ..$ profileId  : chr "ga:25439551"
   ..$ dimensions : chr "ga:date,ga:deviceCategory"
   ..$ metrics    : chr  "ga:users" "ga:sessions" "ga:pageviews"
   ..$ segment    : chr "sessions::condition::ga:pagePath=@/f            /relations     /questions_reponses-best_of/;sessions::condition::ga:pagePath=@/f/manger-bouger/q"| __truncated__

   ..$ start.index: int 1
   ..$ max.results: int 10000
  - attr(*, "sampled")= logi FALSE

I want to make a plot with ggplot2 but i cant access the data in $segment ? Is it possible to convert into dataframe ?

Upvotes: 8

Views: 48714

Answers (3)

Rnoob
Rnoob

Reputation: 1013

df = as.data.frame(gaDataExt)

Simple as that.

Upvotes: 17

Vincent Brandon
Vincent Brandon

Reputation: 19

I had the same problem trying to get data into knn and it telling me that subsets of the wonky import weren't the same length. I ended up creating a for loop to dump each column into a new data frame one by one. The new data frame was clean.

my code:

library(readr)
#Import data to CancerData
cancerData_raw <- read_csv("E:/R/TestDataSets/breast-cancer-wisconsin.data", col_names = FALSE)
#clean data structure.  This read is giving lots of errors and tbl, tbl_df, dataframe types
cancerData <- data.frame(cancerData_raw[,1]) 
for (column in c(2:ncol(cancerData_raw))){
  print(column)
  cancerData <- cbind(cancerData, cancerData_raw[,column])
}

#comparing the structure of the old set vs copied one:
str(cancerData_raw)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   699 obs. of  11 variables:
#vs
str(cancerData)
'data.frame':   699 obs. of  11 variables:

I'm not sure if there's a simpler way of doing that operation. This is pretty terribly inefficient to run on larger sets.

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 174863

If that's an accurate reproduction of the output from str() (and I am not sure because the users, sessions, pageviews seem to have been indented), then the segments component is actually one of several components in a list that is an attribute of the pageviews component.

Assuming the above is correct, then you can access the list containing the segment component via:

attr(gaDataExt$pageviews, "query")$segment

or

attr(gaDataExt[["pageviews"]], "query")$segment

If those attributes are actually attributes of the tbl_df, then access segments via

attr(gaDataExt, "query")$segment

I suspect you want this as the attributes appear to be on the tbl_df ubt the str() output isn't clear.

Upvotes: 0

Related Questions