简体   繁体   English

R语言:括号表示法

[英]R language: bracket notation

For a research project I have a relatively large block of code that is taking quite a while to run.对于一个研究项目,我有一个相对较大的代码块,需要很长时间才能运行。 Need to shorten the time it takes for this program to run, so ran profr to look at what functions are taking up the most time.需要缩短这个程序运行的时间,所以跑profr看看哪些函数占用的时间最多。 Thing is, I don't understand the notation.问题是,我不明白这个符号。 Can someone explain to me, or point me to a resource that explains, what these mean:有人可以向我解释,或向我指出解释这些含义的资源:

[<-.data.frame
[[.data.frame
[<-
[
[.factor
[.data.frame
[<-factor

? ? I realize they must be some sort of internals in R for creating new and subsetting dataframes, I just don't know which.我意识到它们必须是 R 中的某种内部结构,用于创建新的和子集数据帧,我只是不知道是哪个。

Thanks.谢谢。

Quoting from the "R for Dummies" cheet sheat:引自“R for Dummies”小贴士:

Subsetting R Objects子集 R 对象

Vectors, lists, and data frames play an important role in representing data in R, so being able to succinctly and correctly specify a subset of your data is important.向量、列表和数据框在 R 中表示数据方面发挥着重要作用,因此能够简洁而正确地指定数据的子集非常重要。

You can use three operators to subset your data:您可以使用三个运算符来对数据进行子集化:

  • [[ : Extracts a single element by name or position from a list or data frame. [[ :从列表或数据框中按名称或位置提取单个元素。 For example, iris[["Sepal.Length"]] extracts the column Sepal.Length from the data frame iris;例如iris[["Sepal.Length"]]从数据帧iris中提取列Sepal.Length iris[[2]] extracts the second element from iris. iris[[2]]从 iris 中提取第二个元素。

  • [ : Extracts multiple elements from a vector, array, list, or data frame. [ :从向量、数组、列表或数据框中提取多个元素。 For example, iris[, c("Sepal.Length", "Species")] extracts the columns Sepal.Length and Species from iris;例如, iris[, c("Sepal.Length", "Species")]从 iris 中提取列Sepal.LengthSpecies iris[1:10, ] extracts the first ten rows from iris; iris[1:10, ]从 iris 中提取前十行; and iris[1:10, "Species"] extracts the first ten elements of the column Species from iris.iris[1:10, "Species"]从 iris 中提取 Species 列的前十个元素。


You can find the same information in ?Extract although not as nicely summarised ;-)您可以在?Extract找到相同的信息,尽管没有很好地总结;-)


My guess is that your profiling problem is with [<- since I know this is a slow operation.我的猜测是您的分析问题是[<-因为我知道这是一个缓慢的操作。 You possibly have a loop with multiple [<- column assignments into a data frame.您可能有一个循环,其中包含多个[<-列分配到数据框中。 You can make this substantially faster by:您可以通过以下方式大大加快速度:

  • Making a single assignment of multiple columns对多列进行单一分配
  • Using the package data.table使用包data.table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM