How to avoid implicit character conversion when using apply on dataframe

后端 未结 2 1507
情书的邮戳
情书的邮戳 2020-12-31 07:01

When using apply on a data.frame, the arguments are (implicitly) converted to character. An example:

df <- data.frame(v=1:10, t=1:10)
df <         


        
相关标签:
2条回答
  • 2020-12-31 07:10

    Let's wrap up multiple comments into an explanation.

    1. the use of apply converts a data.frame to a matrix. This means that the least restrictive class will be used. The least restrictive in this case is character.
    2. You're supplying 1 to apply's MARGIN argument. This applies by row and makes you even worse off as you're really mixing classes together now. In this scenario you're using apply designed for matrices and data.frames on a vector. This is not the right tool for the job.
    3. In ths case I'd use lapply or sapply as rmk points out to grab the classes of the single t2 column as seen below:

    Code:

    df <- data.frame(v=1:10, t=1:10)
    df <- transform(df, t2 = as.POSIXlt(t, origin = "2013-08-13"))
    
    sapply(df[, "t2"], class)
    lapply(df[, "t2"], class)
    
    ## [[1]]
    ## [1] "POSIXct" "POSIXt" 
    ## 
    ## [[2]]
    ## [1] "POSIXct" "POSIXt" 
    ## 
    ## [[3]]
    ## [1] "POSIXct" "POSIXt" 
    ## 
    ## .
    ## .
    ## . 
    ## 
    ## [[9]]
    ## [1] "POSIXct" "POSIXt" 
    ## 
    ## [[10]]
    ## [1] "POSIXct" "POSIXt" 
    

    In general you choose the apply family that fits the job. Often I personally use lapply or a for loop to act on specific columns or subset the columns I want using indexing ([, ]) and then proceed with apply. The answer to this problem really boils down to determining what you want to accomplish, asking is apply the most appropriate tool, and proceed from there.

    May I offer this blog post as an excellent tutorial on what the different apply family of functions do.

    0 讨论(0)
  • 2020-12-31 07:14

    Try:

    sapply(df, function(y) class(y["t2"]))
    
    $v
    [1] "integer"
    
    $t
    [1] "integer"
    
    $t2
    [1] "POSIXct" "POSIXt"
    
    0 讨论(0)
提交回复
热议问题