简体   繁体   English

从另一个df为df $列分配值?

[英]Assign a value to a df$column from another df?

Example: I have a df in which the first column is 示例:我有一个df,其中第一列是

dat <- c("A","B","C","A")

and then I have another df in which I have in the first column is: 然后我在第一列中有另一个df:

dat2[, 1]
[1] A B C
Levels: A B C

dat2[, 2]
[1] 21000 23400 26800

How can I add the values in the second df ( dat2 ) to the first df ( dat )? 如何将第二个df( dat2 )中的值添加到第一个df( dat )? In the first df there are repetitions and I want that everytime there is an "A" it will add the corresponding value (21000) from the second df in a new column. 在第一个df中有重复,我希望每次有“A”时它会在新列中添加第二个df的相应值(21000)。

Generating reproducible dataframe... 生成可重现的数据帧......

dat1 <- data.frame(x1 = c("A","B","C","A"), stringsAsFactors = FALSE)
dat2 <- data.frame(x1 = c("A","B","C"),
                   x2 = c(21000, 23400, 26800), stringsAsFactors = FALSE)

Then use the match function. 然后使用match功能。

dat1$dat2_vals <- dat2$x2[match(dat1$x1, dat2$x1)]

It is important to transform your character columns to character type rather than factor type or the elements will not match. 将字符列转换为character类型而不是factor类型或元素不匹配非常重要。 I mention this due to the levels attribute in your dat2. 由于dat2中的levels属性,我提到了这一点。

A third option which I prefer is left_join from dplyr ... It seems to be faster than merge with large data frames. 我喜欢第三种选择left_joindplyr ......这似乎是快于merge大数据帧。

require(dplyr)

dat1 <- data.frame(x1 = c("A","B","C","A"), stringsAsFactors = FALSE)
dat2 <- data.frame(x1 = c("A","B","C"),
                   x2 = c(21000, 23400, 26800), stringsAsFactors = FALSE)

dat1 <- left_join(dat1, dat2, by="x1")

Let's race large dataframes with microbenchmark , just for fun! 让我们用microbenchmark比赛大型数据帧,只是为了好玩!

create large dataframes 创建大型数据帧

dat1 <- data.frame(x1 = rep(c("A","B","C","A"), 1000), stringsAsFactors = FALSE)
dat2 <- data.frame(x1 = rep(c("A","B","C", "D"), 1000),
                   x2 = runif(1,0), stringsAsFactors = FALSE)

on your marks, get set, GO! 在你的标记,得到设置,GO!

library(microbenchmark)
mbm <- microbenchmark(
  left_join = left_join(dat1, dat2, by="x1"),
  merge = merge(dat1, dat2, by = "x1"),
  times = 20
)

Many, many seconds later.... left_join is MUCH faster for large dataframes. 很多很多秒钟后.... left_join 快得多大型dataframes。

在此输入图像描述

Use merge function. 使用merge功能。

# Input data
dat  <- data.frame(ID = c("A", "B", "C", "A"))
dat2 <- data.frame(ID = c("A", "B", "C"), 
                   value = c(1, 2, 3))
# Merge two data.frames by specified column
merge(dat, dat2, by = "ID")
  ID value
1  A     1
2  A     1
3  B     2
4  C     3

暂无
暂无

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

相关问题 如何根据另一个df中的值使用向量中的值将值分配给df的列 - How to assign a value to a column of a df using values from a vector according to a value in another df 使用另一个 df 中的列扫描一个 df 中的列,如果满足条件,则分配在第二个 df 中创建一个额外的列并分配值 - Scan a column in one df using column in another df and if condition is met assign the create an extra column in the second df and assign value 将值分配给 df$column,其值在 R 中的另一个 df 中计算 - Assign values to a df$column with values calculated in another df in R 如何使用 R 中的条件从另一个 df 为 df$column 赋值 - How to assign values to a df$column from another df using conditions in R 根据另一个 df 更改 df1 列中的值 - Change value within a df1 column according to another df r - Select DF 基于另一个 DF 列中的值 - r - Select DF based on value in column of another DF 使用带有粘贴的 For 循环将 df 中的值替换为另一个 df 中的值 - Using For Loop with Paste to replace value in df with value from another df 如何将一个 df 列中的每个值与 R 中另一个 df 列中的每个值进行比较? 具有不同行号的dfs - How to compare every value from a column from one df with every value from a column of another df in R? dfs with different row numbers 如果 df1 中的字符串值“X”等于 df2 中的任何字符串值,则将类别“1”分配给 R 中 df1 中新列中的值 X - If string value “X” in df1 is equal to any of the string values in df2, assign category “1” to value X in a new column in df1 in R 用另一个df的值替换na - Replace na's with value from another df
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM