banner



How To Put Data Into R

In this article, we will see how to add dataframe at the cease of another dataframe in R Programming Language.

Method one: Using rbind() method

The rbind() method in R works merely if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a dissimilar number of rows. The grade of the dataframe columns should be consistent with each other, otherwise, errors are thrown. The post-obit properties are maintained :

  • The rows are appended in the order of dataframe appearance during the function call.
  • The total number of columns is equivalent to the columns in whatever of the dataframes.
  • The total number of rows is equivalent to the summation of rows in the dataframe.
rbind ( df1, df2)

Instance: Adding Dataframe using rbind() method

R

data_frame1 <- data.frame (col1 = c (2, 4, 6),

col2 = FALSE ,

col3 = Letters [one : three])

print ( "First Dataframe" )

print (data_frame1)

data_frame2 <- information.frame (col1 = c (1 , 3),

col2 = TRUE ,

col3 = LETTERS [4 : 5])

impress ( "Second Dataframe" )

impress (data_frame2)

print ( "Combining Dataframe" )

rbind (data_frame1, data_frame2)

Output:

Method 2: Using plyr bundle

The "plyr" bundle in R is used to piece of work with information, including its enhancements and manipulations. It can be loaded and installed into the working space past the following command:

install.packages("plyr")

rbind.fill up() method in R is an enhancement of the rbind() method in base of operations R, is used to combine dataframes with different columns. The column names are numbers may exist different in the input dataframes. Missing columns of the corresponding dataframes are filled with NA. The output dataframe contains a cavalcade only if it is nowadays in any of the dataframes.

rbind.make full( df1, df2)

The post-obit properties are maintained past the rbind.make full() method :

  • The dataframes are appended in the lodge of their specification in the function.
  • The total number of columns is equivalent to the summation of the number of columns of both the dataframes.
  • The total number of rows is equivalent to the summation of the number of rows of both the dataframes.
  • The appearance of columns is in the order of dataframe arguments declaration during the office telephone call.
  • Empty cells are created in the missing columns.

Example: Adding Dataframe using plyr parcel

R

library ( "plyr" )

data_frame1 <- data.frame (col1 = c (2, 4, 6),

col2 = c (4, six, 8),

col3 = c (8, 10, 12),

col4 = Messages [1 : 3])

print ( "First Dataframe" )

print (data_frame1)

data_frame2 <- information.frame (col4 = messages [1:four],

col5 = Truthful )

impress ( "Second Dataframe" )

print (data_frame2)

print ( "Combining Dataframe" )

rbind.fill (data_frame1, data_frame2)

Output:

Method three: Using dplyr packet

The "dplyr" package in R is used to work with information, including its enhancements and manipulations. It can exist loaded and installed into the working space by the following command :

install.packages("dplyr")

The bind_rows() method is used to combine dataframes with different columns. The column names are numbers may be dissimilar in the input dataframes. Missing columns of the corresponding dataframes are filled with NA. The output dataframe contains a column only if it is present in whatever of the dataframes.

bind_rows(df1, df2)

The backdrop are like to the working of the "plyr" package. This method just offers an enhancement to information manipulation.

Case: Adding Dataframe using dplyr package

R

library ( "dplyr" )

data_frame1 <- information.frame (col1 = c (2, 4, half dozen),

col2 = c (four, 6, 8),

col3 = c (8, 10, 12),

col4 = c (20, 16, fourteen))

impress ( "Showtime Dataframe" )

print (data_frame1)

data_frame2 <- data.frame (col5 = messages [1 : four],

col6 = TRUE )

print ( "Second Dataframe" )

impress (data_frame2)

print ( "Combining Dataframe" )

bind_rows (data_frame1, data_frame2)

Output:

In example, whatsoever of the column names are the same in both of the input dataframes, then the post-obit properties are encountered :

  • The class of the common column should exist the aforementioned in both the dataframes, otherwise, an error is encountered.

In this case, the total number of columns in the output dataframe should be equivalent to the full input columns minus the intersecting columns.

Example: Adding dataframe with one column having same name.

R

library ( "dplyr" )

data_frame1 <- information.frame (col1 = c (2, 4, six),

col2 = c (4, vi, 8),

col3 = c (8, 10, 12),

col4 = LETTERS [1 : 3])

print ( "Start Dataframe" )

print (data_frame1)

data_frame2 <- information.frame (col4 = letters [1 : four],

col5 = True )

print ( "Second Dataframe" )

print (data_frame2)

print ( "Combining Dataframe" )

bind_rows (data_frame1,data_frame2)

Output:


Source: https://www.geeksforgeeks.org/how-to-add-dataframe-to-dataframe-in-r/

0 Response to "How To Put Data Into R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel