Adding a column with different number of rows

rbind different column number
dataframe with unequal columns
merge dataframe with different number of rows
cbind with unequal rows in r
combine two data frames in r with different columns
how to join tables with different number of rows in r
add column from one dataframe to another r
rbind columns

I have two dataframes that I want to combine but I'm not sure how I would as one of them has a different number of columns. The first dataframe is this one:

Species Name                 Country           Lat         Lon  
Corynosoma hannae            New Zealand     -46.5000     170.3000
Polymorphus brevis           Mexico           19.4206    -102.2060
Acanthocephala terminalis    United States    38.1806    -83.4505
Polymorphus brevis           Mexico           30.5603    -115.9420
Polymorphus brevis           Mexico           19.6728    -99.7078
Polymorphus brevis           Mexico           19.6833    -101.8830
Polymorphus brevis           Mexico           30.5603    -115.9420
Polymorphus brevis           Mexico           30.5603    -115.9420

The second dataframe:

Species Name                 Country          Number of Records
Corynosoma hannae            New Zealand              3
Polymorphus brevis           Mexico                   41 
Acanthocephala terminalis    United States            1

The second dataframe tallies how many species samples were obtained for each country. I want to be able to add Number of Records to the first dataframe mainly be grouping such that the dataframe appears as so:

    Species Name                 Country           Lat         Lon       Number of Records  
    Corynosoma hannae            New Zealand     -46.5000     170.3000         3
    Acanthocephala terminalis    United States    38.1806    -83.4505          1
    Polymorphus brevis           Mexico           30.5603    -115.9420         41
    Polymorphus brevis           Mexico           19.6728    -99.7078
    Polymorphus brevis           Mexico           19.6833    -101.8830
    Polymorphus brevis           Mexico           30.5603    -115.9420
    Polymorphus brevis           Mexico           30.5603    -115.9420
    Acanthocephala confraterna   United States    35.6859    -83.4986           2

So I don't want to repeat, for example 41, for every row of Polymorphus brevis. I want it to just be one row that groups all of the Polymorphus brevis samples found in Mexico into the column Number of Records. Any help with this would be appreciated. I'm trying to create bubblemap with this dataframe using the rworldmap package.

Based on our comments a simpler solution might be to add a new column to dataframe 1 rather that joining two dataframes -

library(dplyr)

df1 %>%
  group_by(`Species Name`, Country) %>%
  mutate(
    nRecords = ifelse(row_number() == 1, n(), NA_integer_)
  ) %>%
  ungroup()

Add or remove rows and columns in Numbers on Mac, How do you insert a column in Excel without messing up formulas? Adding columns or rows in Numbers Adding multiple columns or rows. You may want to prepare for additional data and need more columns or rows in your spreadsheet. You can add multiple columns and rows by doing the following. 1) Click and hold the two-line circled symbol at the top right for columns or bottom left for rows.

Something like:

library(dplyr)

left_join(df1, df2, by = c("Species Name", "Country")) %>%
  group_by(`Species Name`, Country) %>%
  mutate(
    `Number of Records` = as.numeric(as.character(`Number of Records`)),
    `Number of Records` = ifelse(row_number() == 1, `Number of Records`, NA)
    )

Insert or delete rows, and columns - Excel, How many rows and columns are there in Excel 2007? Select the range you will add multiple blank rows or columns between every n rows or columns, and click Kutools > Insert > Insert Blank Rows & Columns. 2 . In the Insert Blank Rows and Columns dialog, please:

I agree with the two previous answers suggesting to simply add a new column to one data frame with info from the other data frame. One way of doing this is by using the function match():

# define 1st df:
df1 <- data.frame( 
  Observations = c("obs1", "obs2", "obs3"),
  Data = c(sample(1:20, 3))
  )

# define 2nd df:
df2 <- data.frame( 
  OtherObservations = c("obs1", "obs2", "obs3"),
  OtherData = c(1, 2, NA)
)

# now add to df1 the relevant column in df2 based on matching data in either data frame:
df1$NewColumn <- df2$OtherData[match(df1$Observations, df2$OtherObservations)]
df1

Excel 2003 vs Excel 2007, Excel provides multiple techniques that you can use to sum data. To help you Add the values in a column or row by using a button. You can  So I don't want to repeat, for example 41, for every row of Polymorphus brevis. I want it to just be one row that groups all of the Polymorphus brevis samples found in Mexico into the column Number of Records. Any help with this would be appreciated. I'm trying to create bubblemap with this dataframe using the rworldmap package.

Microsoft Excel, Insert multiple rows or columns at once: Anywhere in the table, select a number of rows or columns equal to the number of rows or columns you want to insert, click the arrow next to one of the selected row numbers or column letters, then choose Add Rows Above or Add Rows Below (or Add Columns Before or Add Columns I am trying to count, for example the number of occurrences of a particular text , say "SP", in a row from column c to column dd - OK so far. But now I want to only count the occurrences in the row where the date in column A = today().

Insert or delete rows, and columns - Excel - Office support, We can use the below steps to add multiple observations at a time. Create a new Data Frame of the same number of variables/columns with the  Merge table with different rows. Thanks but it is adding extra NAN in each row. two tables with different number of rows (but same number of columns), this

R Data Frame Operations, You might want to add up several of your existing columns, find an average or otherwise calculate some "result" from existing data in each row. There are many​  On the other hand, while adding rows the number of elements in the rows should be equal to the number of columns to the existing data frame. Otherwise, it will give the exception and some of the column value will be added as NA.

Comments
  • Does dataframe 2 simply count the number of records in dataframe 1? If so there will be a simpler way to do this.
  • @Shree dataframe 2 tallies "Species Name" and "Country" which is present in dataframe 1. It shows how many samples of that species were collected from a specific country. Thus DF2 has 38 rows while DF1 has 56.
  • I added an answer based on your above comment. See if it works for you. If not, I'll remove it.
  • I tried this and I got the following error 'Error in mutate_impl(.data, dots) : Evaluation error: comparison (1) is possible only for atomic and list types.'
  • see if my edit helps. If not, add the output of dput(head(your_dataframe)) to your post.
  • I did as you said and I got the following error: 'Error in mutate_impl(.data, dots) : Evaluation error: false must be type character, not double Call rlang::last_error() to see a backtrace.'
  • I tried the edit and i get the following error: 'Error in mutate_impl(.data, dots) : Evaluation error: false must be type integer, not character Call rlang::last_error() to see a backtrace.'
  • I think your Number of Records is probably a character or even factor; I've amended my solution and think this should apply to your dataframe as well regardless of the types.