How to make the first position of a list the heading

So far I have always worked with CSV files that were ordered from top down
at the top the header and below the results.
but now I have a dataset in front of me that is sorted like this

so header and then in the same line the results.

How do I now read the file so that Unknow1, 2.... is my header line and the numerical values are the list belonging to the header line ?

My used read in code was:
data<-read.table(file="XXXXXXX", header=FALSE, sep=";", strip.white = TRUE, stringsAsFactors = TRUE, fileEncoding = "UTF-8", na.strings = "NA", skip=350, nrows=96,dec=",")

One way would be to read in the first column separate from the others and assign the column values (so your actual names) to the names attribute of the other data.frame which contains the values.

Thanks for the answer because I have to do that 1200x, this way is unfortunately associated with too much work

If the structure is always the same, you can wrap this method into a function and just apply it to every file you have to read. You can use list.files to get a character vector of all files in one directory and use lapply to create a list with the correctly formatted tables. So no additional work beside writing the function from this side.

tf1 <- tempfile()

           con = tf1)

tf2 <- tempfile()

           con = tf2)

(my_files_to_fix <- list(tf1,tf2))

fixme <- function(x){
  (interim <- t(read.csv(x,header = FALSE)))
  fixmetemp <- tempfile()
  write.table(x = interim,
              file = fixmetemp,row.names = FALSE,col.names=FALSE,sep=",")
  (happy <- read.csv(tf2))

(good_result <- fixme(tf1))

(my_results <- purrr::map(my_files_to_fix,

Alternate solution. My first four lines are only to get some of your data - you don't need those lines because you already read in your data.

row1 <- c("Unknown1", "121", "113.5", "117.5")
row2 <- c("Unknown2", "75", "68", "83.5")
row3 <- c("Unknown3", "319", "299", "290.5")
m <- rbind(row1, row2, row3)
m_transpose <- t(m)
colnames(m_transpose) = m_transpose[1,]
m_transpose <- m_transpose[-1,]

 Unknown1 Unknown2 Unknown3

[1,] "121" "75" "319"
[2,] "113.5" "68" "299"
[3,] "117.5" "83.5" "290.5"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.