Presentation describing the transpose function in R
Ashu Guru
Author
03/13/2019
Added
112
Plays
Description
Presentation describing the transpose function in R , by Ashu Guru, Univ of Nebraska Raikes School (4 mins)
Searchable Transcript
Toggle between list and paragraph view.
- [00:00:00.480]A common function that we want to apply on a table is transpose.
- [00:00:06.160]Assume we have a table which has three columns, filter on attribute 1,
- [00:00:12.373]attribute 2 and a couple of rows of data separated by commas.
- [00:00:18.400]What we want is we want to transpose the rows into columns and columns into rows.
- [00:00:22.600]So we want that the data should look something like column 110.1.
- [00:00:29.680]So I want that the column header should look like column and whatever is in the
- [00:00:36.966]row one cell one, append it to it and then the second
- [00:00:41.884]column should be column string appended with row 2 cell one.
- [00:00:48.040]And then I want the values to be transposed.
- [00:00:50.920]So I want 100.1, 100.2, 100.1, 100.2. 20 and 30. 20 and 30. 20 and 10.
- [00:00:58.107]20 and 10.
- [00:00:59.120]So let's see what function can we use.
- [00:01:02.200]So as always here is my input table inner text file.
- [00:01:10.520]I have filter on attribute 1, attribute 2 and then I have couple of
- [00:01:15.697]rows of 100.1, 100. 2 and then rest of the column values
- [00:01:20.037]separated by commas.
- [00:01:22.760]So what I do is first step change the working directory to the directory where
- [00:01:28.070]we have the files.
- [00:01:31.720]Next I load the data so I will run the instruction.
- [00:01:37.920]I read the table which is in a file called transpose.CSV.TXT.
- [00:01:44.000]It has headers and the separator is a comma.
- [00:01:47.600]So I am going to run this instruction.
- [00:01:52.480]Next is the function that we apply.
- [00:01:54.880]So here is that function.
- [00:01:59.920]So it has actually transposed the table for us.
- [00:02:06.840]However, in this transpose, R converts this into a matrix.
- [00:02:12.560]I need to reconvert it to table because I like to work with tables.
- [00:02:16.120]In order to do that I will use another command which is called as as data frame
- [00:02:22.605]and then the matrix that I have.
- [00:02:25.200]So I will now transform it as table run this instruction.
- [00:02:31.600]So if I want to see what is the column names of the table that I have as
- [00:02:39.584]transformed, let me type in column names and tab data.
- [00:02:51.030]The column name are V1 and V2.
- [00:02:53.390]What I want to do is I wanted a specific column name so that I can keep an
- [00:02:59.843]indicator in my column name that says what row this comes from.
- [00:03:05.510]So I wanted my column names to look like this and in order to do that what I can
- [00:03:13.091]do is I can use the column names command and assign it the value of the vector
- [00:03:20.485]which is a combination of paste.
- [00:03:23.560]A paste is an R function that joins a string with values of another vector.
- [00:03:30.640]So I have paste.
- [00:03:32.160]Then I want it to join the string column to the first row of the table separated
- [00:03:40.207]by no space.
- [00:03:41.520]So when I run this command and now I run column names,
- [00:03:46.670]you can see that I have column name which is concatenation of the string column and
- [00:03:54.537]then the values in the first row 100. 1 and then 100.2 respectively.
- [00:04:01.360]Let me just print the full table and here is what I was expecting.
- [00:04:06.960]So my columns have been transformed to rows and my rows have been transformed to
- [00:04:12.202]columns.
- [00:04:13.320]In addition, I have changed the column titles.
The screen size you are trying to search captions on is too small!
You can always jump over to MediaHub and check it out there.
Log in to post comments
Embed
Copy the following code into your page
HTML
<div style="padding-top: 56.25%; overflow: hidden; position:relative; -webkit-box-flex: 1; flex-grow: 1;"> <iframe style="bottom: 0; left: 0; position: absolute; right: 0; top: 0; border: 0; height: 100%; width: 100%;" src="https://mediahub.unl.edu/media/10781?format=iframe&autoplay=0" title="Video Player: Presentation describing the transpose function in R" allowfullscreen ></iframe> </div>
Comments
0 Comments