This is a test version of Biostars. For the public version, visit https://www.biostars.org.
transpose rows and columns

I have a dataset in excel/ txt file with 65 columns and 300000 rows. What will be the best way to switch columns and rows. It cannot be done in excel. I tried using R t() it also does not do the trick.

any suggestion?

Thanks

columns transpose rows

t() works on at least some data.frames (and indeed you can transpose sheets in excel, though I'd keep a 300K row sheet out of excel if I were you!). So what error messages to you get when you try to do this transposition?

Here is so far I have used-

dat<-read.delim(file="fILE_2_329515.txt",sep="\t", header=T)
dim(dat)

names(dat)

row.names(dat) <- dat[[1]]

t(dat)

write.table(dat,file="dat2.txt",sep="\t")

It is not changing the rows and columns and does not throw any error

You need to set the result when transposing.

dat <- t(dat)

will work.

My bad.

Thanks it worked

3 answers

I have a blog post containing Python and Perl code for carrying out a transpose on tab-delimited files. In the post, I also linked to a tool called datamash, which can carry out a transpose. Here's the URL: http://davetang.org/muse/2014/09/09/transpose-tool/.

Here's an elegant and fast method in awk: http://stackoverflow.com/questions/1729824/transpose-a-file-in-bash

This is a pretty neat coreutil-like tool for transposing csv and tsv

Log in to answer this question.