Data frame changes from numeric to character

前端 未结 1 1956
天涯浪人
天涯浪人 2021-01-15 10:42

I open my csv file and I control the class of each of my data:

mydataP<-read.csv(\"Energy_protein2.csv\", stringsAsFactors=F) 

apply(mydataP, 2, function         


        
相关标签:
1条回答
  • 2021-01-15 11:25

    That happens because apply converts your data.frame to matrix and those can only have one class in them.

    Try this instead:

    sapply(mydataP, class)
    

    This is the reason you should normally try to avoid using apply on data.frames.

    This behavior is documented in the help file (?apply):

    If X is not an array but an object of a class with a non-null dim value (such as a data frame), apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., a data frame) or via as.array.


    Here's a reproducible example with the built-in iris dataset:

    > apply(iris, 2, function(i) class(i))
    #Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    # "character"  "character"  "character"  "character"  "character" 
    
    > sapply(iris, class)
    #Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    #   "numeric"    "numeric"    "numeric"    "numeric"     "factor" 
    
    > str(iris)
    #'data.frame':  150 obs. of  5 variables:
    # $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
    # $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
    # $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
    # $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
    # $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
    

    As you can see, apply converts all columns to the same class.

    0 讨论(0)
提交回复
热议问题