r - Column is NULL when printed as individual field or column, filled when whole data frame is printed -
when create column of counts using dplyr, appears filled correctly, until try use counts column on own. example: create dataframe:
v1 <- c("test", "test", "test", "test", "testing", "testing","me-tested", "re tested", "re testing") v2 <- c("othertest", "anothertest", "testing", "123", "random stuff", "irrelevant", "tested", "re-test", "tests") v3 <- c("type1", "type2", "type1", "type2", "type3", "type2", "type2", "type2", "type1") df <- data.frame(v1, v2, v3)
then, use dplyr create column of counts:
df$counts <- df %>% group_by(v3) %>% mutate(count = n())
this gives expected result:
> df v1 v2 v3 counts.v1 counts.v2 counts.v3 counts.count 1 test othertest type1 test othertest type1 3 2 test anothertest type2 test anothertest type2 5 3 test testing type1 test testing type1 3 4 test 123 type2 test 123 type2 5 5 testing random stuff type3 testing random stuff type3 1 6 testing irrelevant type2 testing irrelevant type2 5 7 me-tested tested type2 me-tested tested type2 5 8 re tested re-test type2 re tested re-test type2 5 9 re testing tests type1 re testing tests type1 3
but, when try use counts.count column in way, result null:
> df$counts.count null
same result other columns created dplyr. rest of data frame seems normal:
> df$v1 [1] test test test test testing testing me-tested re tested re testing levels: me-tested re tested re testing test test test test testing testing
i totally confused why printing whole df gives me different output printing column of interest. missing here?
if rewind , recreate dataframe , don't assignment print result screen see this:
df %>% group_by(v3) %>% mutate(count = n()) source: local data frame [9 x 4] groups: v3 [3] v1 v2 v3 count <fctr> <fctr> <fctr> <int> 1 test othertest type1 3 2 test anothertest type2 5 3 test testing type1 3 4 test 123 type2 5 5 testing random stuff type3 1 6 testing irrelevant type2 5 7 me-tested tested type2 5 8 re tested re-test type2 5 9 re testing tests type1 3
if assgnment structure rather confused , think might have gotten more informative error if there had been fewer unique values of v1 or v2:
df$counts <- df %>% group_by(v3) %>% mutate(count = n()) # snipped showed str(df) #----- 'data.frame': 9 obs. of 4 variables: $ v1 : factor w/ 9 levels "me-tested","re tested",..: 7 4 6 5 9 8 1 2 3 $ v2 : factor w/ 9 levels "123","anothertest",..: 4 2 8 1 5 3 7 6 9 $ v3 : factor w/ 3 levels "type1","type2",..: 1 2 1 2 3 2 2 2 1 $ counts:classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ , 'data.frame': 9 obs. of 4 variables: ..$ v1 : factor w/ 9 levels "me-tested","re tested",..: 7 4 6 5 9 8 1 2 3 ..$ v2 : factor w/ 9 levels "123","anothertest",..: 4 2 8 1 5 3 7 6 9 ..$ v3 : factor w/ 3 levels "type1","type2",..: 1 2 1 2 3 2 2 2 1 ..$ count: int 3 5 3 5 1 5 5 5 3 ..- attr(*, "vars")=list of 1 .. ..$ : symbol v3 ..- attr(*, "labels")='data.frame': 3 obs. of 1 variable: .. ..$ v3: factor w/ 3 levels "type1","type2",..: 1 2 3 .. ..- attr(*, "vars")=list of 1 .. .. ..$ : symbol v3 .. ..- attr(*, "drop")= logi true ..- attr(*, "indices")=list of 3 .. ..$ : int 0 2 8 .. ..$ : int 1 3 5 6 7 .. ..$ : int 4 ..- attr(*, "drop")= logi true ..- attr(*, "group_sizes")= int 3 5 1 ..- attr(*, "biggest_group_size")= int 5
the format seeing how r displays matrix embedded in dataframe. objects of class table
(and perhaps tbl
?) inherit matrix
-class.
Comments
Post a Comment