I have a dirty dataset that I could not read it with header = T
. After I read and clean it, I would like to use the now first row data as the column name. I tri
header.true <- function(df) {
names(df) <- as.character(unlist(df[1,]))
df[-1,]
}
Test
df1 <- data.frame(c("a", 1,2,3), c("b", 4,5,6))
header.true(df1)
a b
2 1 4
3 2 5
4 3 6
Probably, the data type of the data frame columns are factors. That is why the code you tried didn't work, you can check it using str(df)
:
Use the argument
stringsAsFactors = FALSE
when you import your data:
df <- read.table(text = "V1 V2 V3 V4 V5
col1 col2 col3 col4 col5
row1 2 4 5 56
row2 74 74 3 534
row3 865 768 8 7
row4 68 86 65 87", header = TRUE,
stringsAsFactors = FALSE )
Then you can use your first attempt, then remove your first row if you'd like:
colnames(df) <- df[1,]
df <- df[-1, ]
It will work if your columns are factors or characters:
names(df) <- lapply(df[1, ], as.character)
df <- df[-1,]
Output:
col1 col2 col3 col4 col5
2 row1 2 4 5 56
3 row2 74 74 3 534
4 row3 865 768 8 7
5 row4 68 86 65 87