Add rows to dataframe from another dataframe, based on a vector

add row to dataframe r
combine two data frames in r with different columns

I'd like to add rows to a dataframe based on a vector within the dataframe. Here are the dataframes (df2 is the one I'd like to add rows to; df1 is the one I'd like to take the rows from):


  ID x y
1  1 a 0
2  2 a 0
3  3 a 0
4  4 b 0
5  5 b 0

  ID x y
2  2 a 5
3  3 a 2
4  4 b 3

I'd like to add to df2 any rows that aren't in df1, based on the ID vector. so my output dataframe would look like this:

 ID x y
  1 b 0
  5 b 0
  2 a 5
  3 a 2
  4 b 3

Can anyone see a way of doing this neatly, please? I need to do it for a lot of dataframes, all with different numbers of rows. I've tried using merge or rbind but I haven't been able to work out how to do it based on the vector.

Thank you!

A solution with dplyr:


#  ID x y
#1  2 a 5
#2  3 a 2
#3  4 b 3
#4  1 a 0
#5  5 b 0

pandas.DataFrame.append, Append rows of other to the end of caller, returning a new object. Columns in other that are not in the caller are added as new columns. Parameters. other  Select rows from a data frame based on values in a vector. 0 votes. 1 view. asked Aug 31, 2019 in R Programming by Ajinkya757 (5.3k points) I have data similar to this: dt <- structure (list (fct = structure (c (1L, 2L, 3L, 4L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 4L), .Label = c ("a", "b", "c", "d"), class = "factor"), X = c (2L, 4L, 3L, 2L, 5L, 4L, 7L, 2L, 9L, 1L, 4L, 2L, 5L, 4L, 2L)), .Names = c ("fct", "X"), class = "data.frame", row.names = c (NA, -15L))

You can do the following:

missingIDs <-  which(!df1$ID %in% df2$ID) #check which df1 ID's are not in df2, see function is.element()
df.toadd <- df1[missingIDs,] #define the data frame to add to df2
result <- rbind(df.toadd, df2) #use rbind to add it

  ID x y
1  1 a 0
5  5 b 0
2  2 a 5
3  3 a 2
4  4 b 3

Merge, join, and concatenate, In [1]: df = DataFrame(np.random.randn(10, 4)) In [2]: df Out[2]: 0 1 2 3 0 append a single row to a DataFrame by passing a Series or dict to append, than other open source implementations (like in R). left​: A DataFrame object; right: Another DataFrame object; on: Columns (names) to join on. Add multiple rows in the dataframe using dataframe.append() and Series We can pass a list of series too in dataframe.append() for appending multiple rows in dataframe. So, let’s create a list of series with same column names as dataframe i.e.

What about this one-liner?

rbind(df2, df1[!df1$ID %in% df2$ID,])

  ID x y
2  2 a 5
3  3 a 2
4  4 b 3
1  1 a 0
5  5 b 0

Python Pandas : How to add rows in a DataFrame using dataframe , Add a row from one dataframe to other dataframe using dataframe.append(). Let's create an another dataframe i.e.. To append or add a row to DataFrame, create the new row as Series and use DataFrame.append () method. where the resulting DataFrame contains new_row added to mydataframe. append () is immutable. It does not change the DataFrame, but returns a new DataFrame with the row appended.

DataFrame/Append - Maple Programming Help, DataFrame/Append append a DataFrame or DataSeries to a DataFrame DataFrame Commands; Append a DataFrame to another DataFrame If df2 is a list or a Vector, then its entries will form the new row in the same order as in df2 itself. in df2; this is different from all other modes, where it is based on the order in df1. As you can see based on the output of the RStudio console, we just created a new data object containing our original data plus the vector. Example 2: Add Row to Data Frame by Number of Rows. Another alternative for appending rows to data frames is based on the number of rows of our data frame.

Working with data in a data frame, A data frame can also be created from vectors, with the data.frame function. For example If an item has an identifier such as a unique name, this would be given as just another column. Indexing data frames. As with a matrix, a data frame can be accessed by row and column with [,] . Let's add this as a column. The two DataFrames are not required to have the same set of columns. The append method does not change either of the original DataFrames. Instead, it returns a new DataFrame by appending the original two. Appending a DataFrame to another one is quite simple:

insertRows function, Insert (multiple) rows to a data.frame, possibly coming from another data.frame, with value and row recycling. Vector with data to be inserted, is recycled. Iteratively appending rows to a DataFrame can be more computationally intensive than a single concatenate. A better solution is to append those rows to a list and then concatenate the list with the original DataFrame all at once.

  • Please don't forget to check the answer that solved your problem so that other users can quickly find a good solution.
  • Was always wondering about the difference between df[condition] and df[which(condition)]... I guess these are equivalent but to be sure I always prefer to use which...
  • For simple indexing these are the same - you don't need which() in this case. However, if you wanted to use the indices for other operations, e.g. beyond simple indexing, you might have to resort to which() or match() depending on your goals in order to get the exact placements within your vector.