如何使用R中较小的数据帧初始化大型数据帧中的值?

时间:2018-04-13 17:42:25

标签: r dataframe

我有一个大型数据框,我想使用较小的第二个数据帧中的值初始化它的一些值。例如,引用以下脚本:

# Creating small dataframe.
df.small <- data.frame(matrix("small", nrow = 5, ncol = 5), stringsAsFactors = FALSE)
df.small

# Creating large dataframe.
df.large <- data.frame(matrix("large", nrow = 10, ncol = 10), stringsAsFactors = FALSE)
df.large

# Creating example of desired output.
# Note: this will only work if all values in small dataframe are the same
df.combined <- df.large
df.combined[1:5, 1:5] <- "small"
df.combined

如果我有df.smalldf.large,如何使用df.large的值初始化df.small的索引[1:5,1:5]的值这样我得到的数据框看起来像df.combined

1 个答案:

答案 0 :(得分:1)

IIUC,df.combined[1:5, 1:5] <- df.small[1:5, 1:5]

相关问题