Tips to use data frame in R

Here are some tips to manipulate data frame in R:

Lets say the data frame that we have is:

> data
   code           sector                           industry
1   901     Basic Industries                   Building and Related
2   902     Basic Industries                   Chemicals
3   903     Basic Industries                   Containers
4   905     Basic Industries                   Fertilizers
5   906     Basic Industries                   Forest Products
6   904     Basic Industries                   Metal Fabricators and Distributors
7   991     Basic Industries                   Mining
8   912     Basic Industries                   Multi-Industry Basic
9   910     Basic Industries                   Nonferrous Base Metals
10  907     Basic Industries                   Paper

Now if we want to show only fewer rows from this data frame then we do:

> data[1:n,]

// where n is the number of rows that you want

> data[1:5,]
  code           sector             industry
1  901     Basic Industries      Building and Related
2  902     Basic Industries      Chemicals
3  903     Basic Industries      Containers
4  905     Basic Industries      Fertilizers
5  906     Basic Industries      Forest Products

> data[n1:n2,]

// where n1 is the row you want to start from and n2 is the last row number

> data[3:7,]
  code           sector                           industry
3  903     Basic Industries                    Containers
4  905     Basic Industries                    Fertilizers
5  906     Basic Industries                    Forest Products
6  904     Basic Industries                    Metal Fabricators and Distributors
7  991     Basic Industries                    Mining

If you wish to use only certain columns then:

data[,c("column1","column2",...)]

//where column1,column2 are the name of the columns that you want to display

> data[,c("code","industry")]
   code                           industry
1   901                      Building and Related
2   902                      Chemicals
3   903                      Containers
4   905                      Fertilizers
5   906                      Forest Products
6   904                      Metal Fabricators and Distributors
7   991                      Mining
8   912                      Multi-Industry Basic
9   910                      Nonferrous Base Metals
10  907                      Paper

Now if you wish see any one particular row based on some criteria then:

> data[data$column == value,]

// here you are looking for a data frame for which any column has certain value

> data[data$code==901,]
  code           sector             industry
1  901     Basic Industries     Building and Related

If you want to delete any particular row then do:

> data < - data[-n,]

// where n is the row number you wish to delete

> data < - data[-4,]
> data
   code           sector                           industry
1   901     Basic Industries                  Building and Related
2   902     Basic Industries                  Chemicals
3   903     Basic Industries                  Containers
5   906     Basic Industries                  Forest Products
6   904     Basic Industries                  Metal Fabricators and Distributors
7   991     Basic Industries                  Mining
8   912     Basic Industries                  Multi-Industry Basic
9   910     Basic Industries                  Nonferrous Base Metals
10  907     Basic Industries                  Paper

//you can see that the 4th row is missing from the data frame now
Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*