Data Frame in R Language

Please load the required data set before running the commands given below in R FAQs related to the data frame. As an example for R FAQs about data frame, we are assuming iris data set that is available already in R. At R prompt write data(iris)

Question: How to name or rename a column in a data frame?
Answer: Suppose you want to change/ rename the 3rd column of the data frame, then on R prompt write

> names (iris)[,3] <- “new_name”

Suppose you want to change the second and third column of the data frame

> names(irisi)[c(2,4)] <- c(“A”, “D”)

Note that names(iris) command are used to find the names of each column in a data frame.

Question: How you can determine the column information of a data frame such as the “names, type, missing values” etc.?
Answer: There are two built-in functions in R to find the information about columns of a data frame.

> str(iris)
> summary(iris)

Question: How a data frame can be exported in R so that it can be used in other statistical software?
Answer: Use write.csv command to export the data in comma separated format (CSV).

> write.csv(iris, “iris.csv”, row.names = FALSE)

Question: How one can select a particular row or column of a data frame?
Answer: The easiest way is to use the indexing notation []

Suppose you want to select the first column only, then at R prompt, write

> iris[,1]

Suppose we want to select the first column and also want to put the content in a new vector, then

> new <- iris[,1]

Suppose you want to select different columns, for example, columns 1, 3, and 5, then

> newdata <- iris[, c(1, 3, 5)]

Suppose you want to select a first and third row, then

> iris[c(1,2), ]

Question: How to deal with missing values in a data frame?
Answer: In R language it is easy to deal with missing values. Suppose you want to import a file names “file.csv” that contains missing values represented by a “.” (period), then on R prompt write

> data <- read.csv(“file.csv”, na.string = “.”)

If missing values are represented as “NA” values then write

> dataset <- read.csv(“file.csv”, na.string = “NA”)

For the case of built-in data such (here iris), use

> data <- na.omit(iris)