简体   繁体   中英

Copy Unique values from one data frame to another in R

I have two data frames with totally different column names and values.

Example :

Data Frame 1 ->

company value
A       10
B       11
A       9

Data Frame 2 ->

id     value2
Q       7
W       8
E       9

This question has several parts that I want to achieve:

  1. Extract the unique values of COMPANY column from data frame 1 based on the COMPANY column(Unique companies)

  2. Copy the unique values obtained above into a NEW COLUMN in Data Frame 2 RANDOMLY (only company field)

  3. Merge the two data frames based on the unique value column.(This is only for testing, hence why I need this step)

All help is appreciated!! Thank you in advance.

You could try something like this:

company <- unique(df1$company)
df2$new_column <- sample(company, nrow(df2), replace = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM