Search code examples
rdplyrspread

In R dplyr, why there is a single quote after spread function?


I am trying to spread a column into multiple columns using dplyr spread function. Once spread, the access to the column has a single quote which I want to remove since it comes in the way of my filtering the dataframe

Following is my code

# Create Test Frame
testframe = data.frame(name = c("foo-tt.0","bar-tt.0","dd-tt.0","tt-tt.0"),age=as.numeric(c(40,38,10,8)))
#Pivot using name
testframe_pivot <- testframe %>% spread(name,age)

I need to access the frame like below

testframe_pivot$`bar-tt.0` ## I don't want these quotes
[1] 38

Why I cannot get like (EXPECTED OUTPUT)

> testframe_pivot$bar-tt.0
[1] 38

instead I get

> testframe_pivot$bar-tt.0
Error: object 'tt.0' not found

I understand they are because of the mix of alphabets and other characters but not sure how to get rid of this error

Consequence is..

>name_I_want = c("foo-tt.0")

>select_(testframe_pivot,.dot=name_I_want)
Error in .f(.x[[i]], ...) : object 'foo' not found

Solution

  • The problem with the names is the - sign. Unless you get rid of it, you won't be able to use the $ operator without back ticks. There are two options with base-R:

    Either you correct the names to something syntactically correct for R. make.names works well:

    names(testframe_pivot) <- make.names(names(testframe_pivot))
    testframe_pivot
    #  bar.tt.0 dd.tt.0 foo.tt.0 tt.tt.0
    #1       38      10       40       8
    

    Or you subset with [[ and strings:

    testframe_pivot[['bar-tt.0']]
    #[1] 38