R Data Frame

Data is displayed in the format of the table in the R data frame. There are different data types inside the R data frames. The character can be the first column, second and third can be numeric or logical. However, each column must have the same type of data.

Following are some R data frame characteristics:

  • The column’s name must be non-empty
  • The rows name must be different
  • The data is stored in a data frame can be a factor, character or numeric type.
  • Every column carry the same number of data items.
R Data Frame
R Data Frame

To create a data frame the data.frame() function is used.

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(34, 67, 87),
  c = c(56, 25, 78)
)

df

Output

df1

Summarize the Data

To summarize the data from a Data Frame we can use the summary() function:

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(34, 67, 87),
  c = c(56, 25, 78)
)

df

summary(df)

Output

df2

Access Items of R Data Frame

To access columns from a data frame we can use single brackets [ ], double brackets [[ ]] or $.

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(34, 67, 87),
  c = c(56, 25, 78)
)

df[2]

df[["a"]]

df$a

Output

df3

Add Rows

To add new rows in a Data Frame we can use the rbind() function:

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

dfr <- rbind(df, c("Python", 110, 110))

dfr

Output

1 40

Add Columns

To add new columns in a Data Frame we can use the cbind() function:

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

dfc <- cbind(df, Java = c(1000, 6000, 2000))

dfc

Output

1 41

Remove Rows and Columns

To remove rows and columns in a Data Frame we can use the c() function:

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

dfn <- df[-c(1), -c(1)]

dfn

Output

1 42

Amount of Rows and Columns

To find the amount of rows and columns in a Data Frame we can use the dim() function:

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

dim(df)

Output

1 43

To find the number of columns we can also use the ncol() function and to find the number of rows nrow() function can be used:

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)


ncol(df)
nrow(df)

Output

1 44

R Data Frame Length

To find the number of columns in a Data Frame we can use the length() function (similar to ncol()):

df <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

length(df)

Output

1 45

Combining Data Frames

To combine two or more data frames in R vertically we can use the rbind() function:

df1 <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

df2 <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

dfn <- rbind(df1, df2)
dfn

Output

1 46

And to combine two or more data frames in R horizontally we can use the cbind() function.

df3 <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

df4 <- data.frame (
  a = c("Python", "R", "C++"),
  b = c(100, 150, 120),
  c = c(60, 30, 45)
)

dfnn <- cbind(df3, df4)
dfnn

Output

1 47