如何在Rcpp中访问DataFrame的特定元素?

问题描述 投票:0回答:1

使用 NumericMatrix,可以轻松访问特定元素。例如,我们可以使用 X(i, j) 访问 NumericMatrix X 的第 i 行和第 j 列的元素。 Rcpp::DataFrame 是否有等效的运算符?

我正在寻找是/否答案或一个简短的例子。

r rcpp
1个回答
0
投票

数据框的方式类似,但是 []

# Create a sample DataFrame
df <- data.frame(
  col1 = c(1, 2, 3),
  col2 = c(4, 5, 6),
  col3 = c(7, 8, 9)
)

# Accessing a specific element using row and column indices
element <- df[2, 3]  # Accesses the element in the 2nd row and 3rd column
print(element)  # Output: 8

© www.soinside.com 2019 - 2024. All rights reserved.