How do you delete the header in a dataframe?

前端 未结 7 654
故里飘歌
故里飘歌 2021-01-04 19:40

I want to delete the header from a dataframe that I have. I read in the data from a csv file then I transposed it, but it created a new header that is the name of the file a

7条回答
  •  借酒劲吻你
    2021-01-04 20:07

    As already mentioned not having column names just isn't something that is going to happen with a data frame, but I'm kind of guessing that you don't care so much if they are there you just don't want to see them when you print your data frame? If so, you can write a new print function to get around that, like so:

    > dat <- data.frame(var1=c("A","B","C"),var2=rnorm(3),var3=rnorm(3))
    > print(dat)
      var1       var2       var3
    1    A  1.2771777 -0.5726623
    2    B -1.5000047  1.3249348
    3    C  0.1989117 -1.4016253
    > ncol.print <- function(dat) print(matrix(as.matrix(dat),ncol=ncol(dat),dimnames=NULL),quote=F)
    > ncol.print(dat)
         [,1] [,2]       [,3]      
    [1,] A     1.2771777 -0.5726623
    [2,] B    -1.5000047  1.3249348
    [3,] C     0.1989117 -1.4016253
    

    Your other option it set your variable names to unique amounts of whitespace, for example:

    > names(dat) <- c(" ", "  ", "   ")
    > dat
    
    1 A  1.2771777 -0.5726623
    2 B -1.5000047  1.3249348
    3 C  0.1989117 -1.4016253
    

    You can also write a function do this:

    > blank.names <- function(dat){
    +   for(i in 1:ncol(dat)){
    +     names(dat)[i] <- paste(rep(" ",i),collapse="")
    +   }
    +   return(dat) 
    + }
    > dat <- data.frame(var1=c("A","B","C"),var2=rnorm(3),var3=rnorm(3))
    > dat
      var1        var2       var3
    1    A -1.01230289  1.2740237
    2    B -0.13855777  0.4689117
    3    C -0.09703034 -0.4321877
    > blank.names(dat)
    
    1 A -1.01230289  1.2740237
    2 B -0.13855777  0.4689117
    3 C -0.09703034 -0.4321877
    

    But generally I don't think any of this should be done.

提交回复
热议问题