How do you delete the header in a dataframe?

前端 未结 7 642
故里飘歌
故里飘歌 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:06

    A function that I use in one of my R scripts:

    read_matrix <- function (csvfile) {
        a <- read.csv(csvfile, header=FALSE)
        matrix(as.matrix(a), ncol=ncol(a), dimnames=NULL)
    }
    

    How to call this:

    iops_even <-  read_matrix('even_iops_Jan15.csv')
    iops_odd  <-  read_matrix('odd_iops_Jan15.csv')
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 20:14

    You could use

    setNames(dat, rep(" ", length(dat)))
    

    where dat is the name of the data frame. Then all columns will have the name " " and hence will be 'invisible'.

    0 讨论(0)
  • 2021-01-04 20:16

    Set names to NULL

    names(df) <- NULL
    

    You can also use the header option in read.csv

    0 讨论(0)
  • 2021-01-04 20:18

    If you really, really, really don't like column names, you may convert your data frame to a matrix (keeping possible coercion of variables of different class in mind), and then remove the dimnames.

    dd <- data.frame(x1 = 1:5, x2 = 11:15)
    mm1 <- as.matrix(dd)
    mm2 <- matrix(mm1, ncol = ncol(dd), dimnames = NULL)
    

    I add my previous comment here as well:
    ?data.frame: "The column names should be non-empty, and attempts to use empty names will have unsupported results.".

    0 讨论(0)
  • 2021-01-04 20:18

    You can use names(df) to change the names of header or col names. If newnames is a list of names as newname<-list("col1","col2","col3"), then names(df)<-newname will give you a data with col names as col1 col2 col3.

    As @ Henrik said, the col names should be non-empty. Setting the names(df)<-NULLwill give NA in col names.

    If your data is csv file and if you use header=TRUE to read the data in R then the data will have same colnames as csv file, but if you set the header=FALSE, R will assign the colnames as V1,V2,...and your colnames in the original csv file appear as a first row.

    anydata.csv  
          a b c  d
        1 1 2 3 13
        2 2 3 1 21
    
    read.csv("anydata.csv",header=TRUE)
          a b c  d
        1 1 2 3 13
        2 2 3 1 21
    
    read.csv("anydata.csv",header=FALSE)
    
      V1 V2 V3 V4
    1  a  b  c  d
    2  1  2  3 13
    3  2  3  1 21
    
    0 讨论(0)
提交回复
热议问题