简体   繁体   English

将一个data.frame的值分配给R中另一个data.frame的特定列?

[英]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 .我想将DF2Z列的第一个值替换为DF1B列的最后一个值。 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 ).我想让它通用,这意味着,不是指定DF1B的最后一行(行号 10),而是有一种方法可以使用end或任何其他可以获取特定列的最后一个值的方法(在这种情况下DF1B列)。

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.

相关问题 在R中的条件下,将一个data.frame中的列值乘以另一个data.frame中的列 - Multiply column values in one data.frame by column in another data.frame on a condition in R 使用一个data.frame中的数据为R中另一个data.frame中的新列生成值 - Using data in one data.frame to generate values for a new column in another data.frame in R 将一个数据框的细胞带信息分配给另一数据框的基因组位置 - Assign cytoband information from one data.frame to genomic position from another data.frame 根据 data.frame 列值分配点颜色 R - Assign point color depending on data.frame column value R 如何基于来自另一个data.frame的用户ID记录为data.frame中的用户分配值 - how to assign value to users in a data.frame based on user ID records from another data.frame 根据行和列元数据将 data.frame 中的一个值除以备用 data.frame 中的另一个值 - Divide one value in a data.frame by another in an alternate data.frame base on row and column meta data 按顺序将 data.frame 中的所有特定值替换为另一个 data.frame 中的值 R - Replace all specific values in data.frame with values from another data.frame sequentially R R如何使用另一个data.frame中的值更新data.frame中的列 - R How to update a column in data.frame using values from another data.frame 使用R中另一个data.frame中的两列重新排序一个data.frame - Reorder one data.frame using two columns from another data.frame in R 在 R 的 data.frame 中的另一列下方添加一列 - Add one column below another in a data.frame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM