简体   繁体   English

将唯一值从一个数据框中复制到 R 中的另一个

[英]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 ->数据帧 1 ->

company value
A       10
B       11
A       9

Data Frame 2 ->数据框 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)根据 COMPANY 列(唯一公司)从数据框 1 中提取 COMPANY 列的唯一值

  2. Copy the unique values obtained above into a NEW COLUMN in Data Frame 2 RANDOMLY (only company field)将上面获得的唯一值随机复制到数据框 2 中的新列中(仅限公司字段)

  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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将唯一值从一列复制到另一列 - Copy unique values from one column to another R 如何将行从一个 data.frame 复制到另一个 [R] - How to copy row from one data.frame in to another [R] 如何在一个数据框中找到唯一值并使用它来过滤 R 中的另一个数据框 - How to find unique values in one data frame and use it to filter another data frame in R 对于R中的data.frame,根据来自另一个数据帧的值从一个数据帧提取数据 - For data.frame in R, pulling data from one data frame based on values from another data frame r-dplyr:计算同一数据帧中另一个变量的每个唯一值的一个变量中唯一值的频率 - r - dplyr: counting the frequency of unique values in one variable for each unique value of another variable in the same data frame R创建一个数据框,总结来自另一个数据框的每列唯一值并排序 - R Create a data frame summarising per column unique values from another data frame and sort 将两列值从一个数据帧复制到一列,但在另一数据帧中复制两行 - Copy two column values from one data frame to one column but two rows in another data frame R dplyr 将一个数据帧中的唯一值替换为具有不同行号的其他数据帧中的唯一值 - R dplyr Replace unique values from one data frame with unique values from other data frame with unequal row numbers 使用 grep 将 R 中一个数据帧中的值替换为另一个数据帧 - Using grep to substitute values from one data frame to another in R 在 R 中,有没有办法用另一个数据帧的值重新编码一个数据帧的列? - In R is there a way to recode the columns from one data frame with values from another data frame?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM