[英]Assign value from one data.frame to a specific column of another data.frame in R?
I would like to replace the first value of column Z
of DF2
with the last value of column B
of DF1
.我想将
DF2
的Z
列的第一个值替换为DF1
的B
列的最后一个值。 I want to make it general, that means, instead of specifying the last row (row number 10) of DF1
column B
, is there a way to use end
or anything else that would grab the last value of a particular column (in this case column B
of DF1
).我想让它通用,这意味着,不是指定
DF1
列B
的最后一行(行号 10),而是有一种方法可以使用end
或任何其他可以获取特定列的最后一个值的方法(在这种情况下DF1
的B
列)。
library(tidyverse)
set.seed(1500)
DF1 <- data.frame(A = runif(10,1,5), B = runif(10,5,10))
DF2 <- data.frame(X = runif(10,1,5), Z = runif(10,5,10))
DF2[1,2] <- DF1$B[10, 2]
I believe this can help you:我相信这可以帮助你:
DF2$Z[1]<-DF1$B[dim(DF1)[1]]
We can use nrow(DF1)
.我们可以使用
nrow(DF1)
。 Either extract using the column index or column name with [[
and then with numeric index for first ( 1
) and last ( nrow
), do the assignment使用带有
[[
的列索引或列名进行提取,然后使用第一个( 1
)和最后一个( nrow
)的数字索引,执行分配
DF2[[2]][1] <- DF1[[2]][nrow(DF1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.