You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已知R赋值采用Copy-on-Modify,切片操作是否同样遵循该机制?

Great question—this cuts right to how R manages memory with its Copy-on-Modify (CoM) rules. Let's break this down for each object type clearly:

Vectors

When you slice a vector, R does NOT create an immediate copy. Instead, the sliced result is a reference to the original vector's memory. Copying only happens when you modify the sliced object.

You can verify this with tracemem(), which tracks when an object is copied:

# Create a vector and track its memory
x <- 1:5
tracemem(x)
# Output: "<0x7f8b1a0b3a80>" (your address will differ)

# Slice to get first 3 elements
y <- x[1:3]
tracemem(y)
# This will show the SAME address as x—proof it's a reference

# Now modify the sliced vector
y[1] <- 100
# You'll see a message like:
# tracemem[0x7f8b1a0b3a80 -> 0x7f8b1a0b3c00]: 
# This means R just created a copy of y (new address) to avoid modifying the original x

Lists

Lists are a bit trickier because they're containers that hold references to other objects. When you slice a list:

  • You get a new list container (it has its own memory address), but
  • The elements inside this new list still reference the original list's elements.

Copying only triggers when you modify an element in the sliced list (or modify the internal content of an element, if it's a mutable type):

my_list <- list(a = 1:3, b = "hello")
tracemem(my_list)
# Output: "<0x7f8b1a0b3d80>"

# Slice to keep the first element
sub_list <- my_list[1]
tracemem(sub_list)
# This shows a NEW address (sub_list is a new container)

# Check if the element references the original
tracemem(sub_list$a)
# Same address as my_list$a—still a reference

# Modify the element inside sub_list
sub_list$a[1] <- 99
# Now sub_list$a gets copied (new address), but my_list$a remains unchanged

Data Frames

Data frames are essentially lists of column vectors, so their slicing behavior mirrors lists:

  • A sliced data frame is a new container (new memory address), but
  • Its columns are still references to the original data frame's columns.

Copying happens only when you modify a column (or elements within a column) in the sliced data frame:

df <- data.frame(x = 1:5, y = 6:10)
tracemem(df)
# Output: "<0x7f8b1a0b3f00>"

# Slice to get first 3 rows
sub_df <- df[1:3, ]
tracemem(sub_df)
# New address—sub_df is a new container

# Check column reference
tracemem(sub_df$x)
# Same address as df$x

# Modify a value in sub_df
sub_df$x[1] <- 99
# sub_df$x is copied (new address), df$x stays as 1:5

Quick Summary

  • Vectors: Sliced result is a reference to the original; copy on modification of the sliced vector.
  • Lists: Sliced result is a new container with references to original elements; copy on modification of elements.
  • Data Frames: Sliced result is a new container with references to original columns; copy on modification of columns/column elements.

内容的提问来源于stack exchange,提问作者JiaHao Xu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:11:34