English 中文(简体)
R语语言的转让
原标题:Assignment in R language

我想知道任务如何用R语进行。

审议以下R shell 会话:

> x <- c(5, 6, 7)
> x[1] <- 10
> x
[1] 10 6 7
>

which I totally understand. The vector (5, 6, 7) is created and bound to the symbol x . Later, x is rebound to the new vector (10, 6, 7) because vectors are immutable data structures.

但这里会发生什么:

> c(4, 5, 6)[1] <- 10
Error in c(4, 5, 6)[1] <- 10 :
  target of assignment expands to non-language object
>

此处或此处:

> f <- function() c(4, 5, 6)
> f()[1] <- 10
Error in f()[1] <- 10 : invalid (NULL) left side of assignment
>

在我看来,人们只能将数值分配给指定数据结构(如xx)。

The reason why I am asking is because I try to implement the R language core and I am unsure how to deal with such assignments.

提前感谢

最佳回答

在我看来,人们只能将数值分配给指定数据结构(如xx)。

?"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

说明:

 Assign a value to a name.

x[1] < - - 10 不使用与 x <- c(5、6、7) 相同的功能。 前者调用 [<- ,而后者调用

问题回答

@Owens 回答 < a href=" https://stackoverflow.com/ questions/10449366/levels-what-sorcery-is-this > 这个问题 , x[1] <- 10 确实有两件事要做。 它调用 [<- 函数, 它将此调用结果指派为 x 。

您想要实现 c( 4, 5, 6, 6) [1] & lt; - 10 结果的是 :

> `[<-`(c(4, 5, 6),1, 10)
[1] 10  5  6

您可以修改匿名函数, 但是没有指定匿名矢量 。 即使 R 也创建了带有名称的临时副本, 您有时会看到反映这个事实的错误信息 。 您可以在 < a href=" http:// cran.r- project. org/ doc/ manuals/ R- lang.pdf\ rel= “ nofollow noreferrer” >R 语言定义 < /a> 第 21页上读到这一点, 其中它涉及“ 子集转让” 和其他形式转让的表达式评估 :

x[3:5] <- 13:15 
# The result of this commands is as if the following had been executed 
`*tmp*` <- x 
x <- "[<-"(`*tmp*`, 3:5, value=13:15) 
rm(`*tmp*`) 

警告不要使用 作为对象名称, 因为下次调用 [<- 时该名称会高写 [<-





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...