How to remove a header in a data frame?

I want to remove the header from the data frame that I have. I read the data from the csv file, after which I transferred it, but created a new header, which is the name of the file and the line from which the data is in the file.

Here is an example for dataframe df:

a.csv.1 a.csv.2 a.csv.3 ... x 5 6 1 ... y 2 3 2 ... 

I want to delete the line a.csv.n, but when I try df <- df[-1,] deletes the row x, not the top one.

+6
source share
6 answers

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

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

I will also add my previous comment:
?data.frame : "Column names must not be empty, and attempts to use empty names will have unsupported results."

+9
source

Set NULL Names

 names(df) <- NULL 

You can also use the header option in read.csv

+5
source

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

As Henrik said, col names must be non-empty. Setting names(df)<-NULL will give NA in the col names.

If your data is a csv file, and if you use header = TRUE to read data in R, then the data will have the same code names as a csv file, but if you set the header = FALSE, R will assign the command names as V1, V2 ,. .. and your colnames in the csv source file are displayed as the first line.

 anydata.csv abcd 1 1 2 3 13 2 2 3 1 21 read.csv("anydata.csv",header=TRUE) abcd 1 1 2 3 13 2 2 3 1 21 read.csv("anydata.csv",header=FALSE) V1 V2 V3 V4 1 abcd 2 1 2 3 13 3 2 3 1 21 
+3
source

you can use

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

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

+3
source

As already mentioned, having no column names is simply not what will happen to the data frame, but I kind of guess that you don't care if they are there, you just don't want to see them when printing the data frame? If so, you can write a new print function to get around this, for example:

 > 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 

Another option: it sets variable names to unique numbers of spaces, 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:

 > 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 usually I donโ€™t think it should be done.

+1
source

The 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 does is called:

 iops_even <- read_matrix('even_iops_Jan15.csv') iops_odd <- read_matrix('odd_iops_Jan15.csv') 
0
source

Source: https://habr.com/ru/post/951499/


All Articles