# [R] transpose rows and columns for large data

Marc Schwartz marc_schwartz at me.com
Tue Nov 29 20:54:11 CET 2016

```Hi,

To provide a [very] small example of what Bert is referring to:

DF <- data.frame(Letters = letters[1:4], int = 1:4)

> str(DF)
'data.frame':	4 obs. of  2 variables:
\$ Letters: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
\$ int    : int  1 2 3 4

> DF
Letters int
1       a   1
2       b   2
3       c   3
4       d   4

DFt <- t(DF)

> str(DFt)
chr [1:2, 1:4] "a" "1" "b" "2" "c" "3" "d" "4"
- attr(*, "dimnames")=List of 2
..\$ : chr [1:2] "Letters" "int"
..\$ : NULL

> DFt
[,1] [,2] [,3] [,4]
Letters "a"  "b"  "c"  "d"
int     "1"  "2"  "3"  "4"

Note the change in the structure  and the data types of the data frame after the transposition...

A data frame, which is a special type of 'list', may contain multiple data types, one per column. It is designed, more or less, specifically to be able to handle multiple data types across the columns, as you might have in a database (character, numeric, date, etc.), but at the expense of some operations.

A matrix, which is in reality a vector with dimensions, can only contain a single data type and in this example, the numeric column is coerced to character. Note also that the "Letters" column in "DF" is changed from being a factor to character as well. So both columns are affected by the transposition.

Regards,

Marc Schwartz

> On Nov 29, 2016, at 1:33 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>
> No, no. It *is* for transposing. But it is *what* you are transposing
> -- a data frame -- that may lead to the problems. You will have to
> read what I referred you to and perhaps spend time with an R tutorial
> or two (there are many good ones on the web) if your R learning is not
> yet sufficient to understand what they say.
>
> -- Bert
>
>
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Tue, Nov 29, 2016 at 11:01 AM, Elham - <ed_isfahani at yahoo.com> wrote:
>> excuse me I did not understand,you mean this function is not for
>> transposing? what function do you suggest?
>>
>>
>> On Tuesday, November 29, 2016 10:24 PM, Bert Gunter <bgunter.4567 at gmail.com>
>> wrote:
>>
>>
>> It is probably worth mentioning that this (i.e. transposing a data
>> frame) can be a potentially disastrous thing to do in R, though the
>> explanation is probably more than you want to know at this point (see
>> ?t  and follow the 'as.matrix' link for details).  But if you start
>> getting weird results and error/warning messages when working with
>> your transposed data, at least you'll know why.
>>
>> Cheers,
>> Bert
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Tue, Nov 29, 2016 at 10:37 AM, Elham - via R-help
>> <r-help at r-project.org> wrote:
>>> thank you all,it worked
>>>
>>>   On Tuesday, November 29, 2016 9:49 PM, "Dalthorp, Daniel"
>>> <ddalthorp at usgs.gov> wrote:
>>>
>>>
>>> Try David's suggestion to spell the argument "stringsAsFactors"
>>> correctly. Then:
>>>
>>> data <- read.table("your_file_location", sep ="\t", comment.char = "",
>>> stringsAsFactors = F, header = T)
>>> transpose_data <- t(data)
>>>
>>> -Dan
>>> On Tue, Nov 29, 2016 at 9:56 AM, Elham - via R-help <r-help at r-project.org>
>>> wrote:
>>>
>>> yes you have right about excel.by R,what should I do for transposing row
>>> and column?
>>>
>>>   On Tuesday, November 29, 2016 9:13 PM, David Winsemius
>>> <dwinsemius at comcast.net> wrote:
>>>
>>>
>>>
>>>> On Nov 29, 2016, at 9:22 AM, Elham - via R-help <r-help at r-project.org>
>>>> wrote:
>>>>
>>>> Hi,
>>>>
>>>> I am trying to transpose large datasets inexcel (44 columns and 57774
>>>> rows) but it keeps giving me the message we can'tpaste because copy area and
>>>> paste area aren't the same size. Is there a way totranspose all the data at
>>>> one time instead of piece by piece? One dataset has agreat amount of rows
>>>> and columns.
>>>>
>>>> I tried this R function to transpose the datamatrix:
>>>>
>>>> data <- read.table("your_file_ location", sep ="\t", comment.char = "",
>>>> stringAsFactors = F, header = T)
>>>>
>>>>
>>>>
>>>> transpose_data <- t(data)
>>>>
>>>> But I received tis error:
>>>>
>>>> unused argument (stringAsFactors = F)
>>>>
>>>
>>> You misspelled that argument's name. And do learn to use FALSE and TRUE.
>>>
>>>>
>>>> Is there another way (I prefer a way with Excel)?
>>>
>>> This is not a help list for Excel.
>>>
>>>
>>> --
>>>
>>> David Winsemius
>>> Alameda, CA, USA
>>>
>>>
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________ ________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/ listinfo/r-help
>>> posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>>
>>> --
>>> Dan Dalthorp, PhDUSGS Forest and Rangeland Ecosystem Science Center
>>> Forest Sciences Lab, Rm 189
>>> 3200 SW Jefferson Way
>>> Corvallis, OR 97331
>>> ph: 541-750-0953
>>
>>> ddalthorp at usgs.gov
>>>
>>>
>>>
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help