Re: [R] how to assign a value to a specific position of a list

2017-05-01 Thread Jeff Newmiller
The only thing "compelling" about your example is that you have the 
pre-conceived (wrong) notion that you have to store your objects under 
separate names in your global environment. That is not only wrong, it 
handicaps you for future unified processing of these data.


I see a lot of constructs in this code that I would change if there was a 
reproducible example here... but just reading code out of context all I 
feel like doing is replacing your uses of assign.


EEM <- list()
for (i in dir()) {
  EEM[[ i ]] <- list()
  fn <- list.files(path = i, pattern = "ASC")
  tr <- sort(as.numeric(unique(unlist(lapply(strsplit(fn, "-"),"[[",
 2)
  for (j in 1:length(tr)) {
fn_tr <- list.files(path = i, pattern = paste(i, tr[j], "...ASC",
  sep="-"))
EEM_tmp <- matrix(NA,ncol = length(fn_tr),nrow = 371)
for (k in 1:length(fn_tr)) {
  data_tmp <- read.csv(paste(i,fn_tr[k],sep="/"), header = FALSE)
  if (dim(data_tmp)[1] != 371) next
  EEM_tmp[,k] <- data_tmp[,2]
}
EEM[[ i ]][[ j ]] <- EEM_tmp
  }
}


On Tue, 2 May 2017, Jinsong Zhao wrote:


Thank you very much, and your reply is helpful.

I don't like assign, and even don't use parse in my previous codes. However, 
in the case I encountered, assign and parse may be the right tools. Here is 
the code I used:


# in the workspace, there are tens directory.
# In each directory, there are lots of *.ASC file,
# with second column is the data.
# Each *.ASC file has a name with pattern i-tr-??.ASC.
# i is the directory name, tr is a group name, and ?? are the index.
# I have to collect all tr-?? into a matrix,
# and put all i-tr-?? into a list EEM_i.

for (i in dir()) {
  assign(paste("EEM_",i,sep=""), list())
  fn <- list.files(path = i, pattern = "ASC")
  tr <- sort(as.numeric(unique(unlist(lapply(strsplit(fn, "-"),"[[", 2)
  for (j in 1:length(tr)) {
 fn_tr <- list.files(path = i, pattern = paste(i, tr[j], "...ASC", 
sep="-"))

 EEM_tmp <- matrix(NA,ncol = length(fn_tr),nrow = 371)
 for (k in 1:length(fn_tr)) {
data_tmp <- read.csv(paste(i,fn_tr[k],sep="/"), header = FALSE)
if (dim(data_tmp)[1] != 371) next
EEM_tmp[,k] <- data_tmp[,2]
 }
 eval(parse(text=paste("EEM_",i,"[[",j,"]]<-","EEM_tmp", sep="")))
  }
}

Any alternatives or improvements? Thanks a lot.

Best,
Jinsong

On 2017/4/30 23:48, peter dalgaard wrote:
assign(paste("list_", i, "[[1]]", sep = ""), 5) creates a new variable with 
a funny name.


You'd have to parse() and eval() to make that work, something like

eval(parse(text=paste("list_",i,"[[1]]<-",5, sep="")))

However,
---

fortunes::fortune("parse")


If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)
---

It is much easier to handle this using a data structure containing a list 
of lists:


l <- rep(list(list()), 10)
for ( i in 1:10 )
   l[[i]][[1]] <- 5


On 30 Apr 2017, at 17:17 , Jinsong Zhao  wrote:

Hi there,

I have a problem with assign(). Here is the demo code:

for (i in 1:10) {
  # create a list with variable name as list_1, list_2, ..., etc.
  assign(paste("list_", i, sep = ""), list())
  # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
  # list_1[[1]] <- 5 # works, however
  assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
}

How to do? Is there any alternatives? Many thanks!

Best,
Jinsong



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value to a specific position of a list

2017-05-01 Thread Jinsong Zhao

Thank you very much, and your reply is helpful.

I don't like assign, and even don't use parse in my previous codes. 
However, in the case I encountered, assign and parse may be the right 
tools. Here is the code I used:


# in the workspace, there are tens directory.
# In each directory, there are lots of *.ASC file,
# with second column is the data.
# Each *.ASC file has a name with pattern i-tr-??.ASC.
# i is the directory name, tr is a group name, and ?? are the index.
# I have to collect all tr-?? into a matrix,
# and put all i-tr-?? into a list EEM_i.

for (i in dir()) {
   assign(paste("EEM_",i,sep=""), list())
   fn <- list.files(path = i, pattern = "ASC")
   tr <- sort(as.numeric(unique(unlist(lapply(strsplit(fn, "-"),"[[", 
2)

   for (j in 1:length(tr)) {
  fn_tr <- list.files(path = i, pattern = paste(i, tr[j], "...ASC", 
sep="-"))

  EEM_tmp <- matrix(NA,ncol = length(fn_tr),nrow = 371)
  for (k in 1:length(fn_tr)) {
 data_tmp <- read.csv(paste(i,fn_tr[k],sep="/"), header = FALSE)
 if (dim(data_tmp)[1] != 371) next
 EEM_tmp[,k] <- data_tmp[,2]
  }
  eval(parse(text=paste("EEM_",i,"[[",j,"]]<-","EEM_tmp", sep="")))
   }
}

Any alternatives or improvements? Thanks a lot.

Best,
Jinsong

On 2017/4/30 23:48, peter dalgaard wrote:

assign(paste("list_", i, "[[1]]", sep = ""), 5) creates a new variable with a 
funny name.

You'd have to parse() and eval() to make that work, something like

eval(parse(text=paste("list_",i,"[[1]]<-",5, sep="")))

However,
---

fortunes::fortune("parse")


If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)
---

It is much easier to handle this using a data structure containing a list of 
lists:

l <- rep(list(list()), 10)
for ( i in 1:10 )
   l[[i]][[1]] <- 5


On 30 Apr 2017, at 17:17 , Jinsong Zhao  wrote:

Hi there,

I have a problem with assign(). Here is the demo code:

for (i in 1:10) {
  # create a list with variable name as list_1, list_2, ..., etc.
  assign(paste("list_", i, sep = ""), list())
  # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
  # list_1[[1]] <- 5 # works, however
  assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
}

How to do? Is there any alternatives? Many thanks!

Best,
Jinsong



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value to a specific position of a list

2017-05-01 Thread MacQueen, Don
It's not clear what you're trying to do. However, to "assign a value to a 
specific position of a list", this example should show you how.

lst <- vector('list', 10)   ## see the help page for list
names(lst) <- paste0('list.',1:10)

## to assign 'a' to position 3:
pos <- 3
lst[[pos]] <- 'a'

I completely agree with Jeff Newmiller's recommendation to avoid using assign. 
It's probably the wrong tool for what you're trying to do (whatever that is).
(and note that I have borrowed Jeff's name "lst" for the list with 10 elements 
[not variables] whose names are "list_1" "list_2" etc.)
(and I refuse to use "_" in R object names, but that's a personal preference)

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062


On 4/30/17, 8:17 AM, "R-help on behalf of Jinsong Zhao" 
 wrote:

Hi there,

I have a problem with assign(). Here is the demo code:

for (i in 1:10) {
# create a list with variable name as list_1, list_2, ..., etc.
assign(paste("list_", i, sep = ""), list())
# I hope to assign 5 to list_?[[1]], but I don't know how to code it.
# list_1[[1]] <- 5 # works, however
assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
}

How to do? Is there any alternatives? Many thanks!

Best,
Jinsong

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread Bert Gunter
... and moreover, note that the assignment can even be shortened to:


> for ( i in 1:10 )  l[[c(i,1)]] <- 5

?"[["  contains details, but the relevant point is:

"[[ can be applied recursively to lists, so that if the single index i
is a vector of length p, alist[[i]] is equivalent to
alist[[i1]]...[[ip]] providing all but the final indexing results in a
list."

For a less terse version, see any good online R tutorial. Lists are
extremely useful in R, and indexing is fundamental. If you haven't
spent the time to learn about these constructs, you should now before
posting further. You'll save yourself a  lot of grief and perhaps even
some embarassment.


Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Apr 30, 2017 at 8:48 AM, peter dalgaard  wrote:
> assign(paste("list_", i, "[[1]]", sep = ""), 5) creates a new variable with a 
> funny name.
>
> You'd have to parse() and eval() to make that work, something like
>
> eval(parse(text=paste("list_",i,"[[1]]<-",5, sep="")))
>
> However,
> ---
>> fortunes::fortune("parse")
>
> If the answer is parse() you should usually rethink the question.
>-- Thomas Lumley
>   R-help (February 2005)
> ---
>
> It is much easier to handle this using a data structure containing a list of 
> lists:
>
> l <- rep(list(list()), 10)
> for ( i in 1:10 )
>l[[i]][[1]] <- 5
>
>> On 30 Apr 2017, at 17:17 , Jinsong Zhao  wrote:
>>
>> Hi there,
>>
>> I have a problem with assign(). Here is the demo code:
>>
>> for (i in 1:10) {
>>   # create a list with variable name as list_1, list_2, ..., etc.
>>   assign(paste("list_", i, sep = ""), list())
>>   # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
>>   # list_1[[1]] <- 5 # works, however
>>   assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
>> }
>>
>> How to do? Is there any alternatives? Many thanks!
>>
>> Best,
>> Jinsong
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd@cbs.dk  Priv: pda...@gmail.com
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread Jeff Newmiller
My reaction is... why do you think this is a good approach to pursue?

Avoid using assign!

library( fortunes )
fortune( 236 )

If you really need another level of containment, put your multiple lists into 
another list:

lst  <- lapply( 1:10, list )
lst[[1]][[1]] <- 5

-- 
Sent from my phone. Please excuse my brevity.

On April 30, 2017 8:20:17 AM PDT, Jinsong Zhao  wrote:
>On 2017/4/30 23:17, Jinsong Zhao wrote:
>> Hi there,
>>
>> I have a problem with assign(). Here is the demo code:
>>
>> for (i in 1:10) {
>># create a list with variable name as list_1, list_2, ..., etc.
>>assign(paste("list_", i, sep = ""), list())
>># I hope to assign 5 to list_?[[1]], but I don't know how to code
>it.
>># list_1[[1]] <- 5 # works, however
>>assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
>  # wrong code in previous message, the correct on should be:
> assign(paste("list_", i, "[[1]]", sep = ""), 5) # does not work...
>> }
>>
>> How to do? Is there any alternatives? Many thanks!
>>
>> Best,
>> Jinsong
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread peter dalgaard
assign(paste("list_", i, "[[1]]", sep = ""), 5) creates a new variable with a 
funny name. 

You'd have to parse() and eval() to make that work, something like

eval(parse(text=paste("list_",i,"[[1]]<-",5, sep="")))

However,
---
> fortunes::fortune("parse")

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
  R-help (February 2005)
---

It is much easier to handle this using a data structure containing a list of 
lists:

l <- rep(list(list()), 10)
for ( i in 1:10 ) 
   l[[i]][[1]] <- 5
 
> On 30 Apr 2017, at 17:17 , Jinsong Zhao  wrote:
> 
> Hi there,
> 
> I have a problem with assign(). Here is the demo code:
> 
> for (i in 1:10) {
>   # create a list with variable name as list_1, list_2, ..., etc.
>   assign(paste("list_", i, sep = ""), list())
>   # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
>   # list_1[[1]] <- 5 # works, however
>   assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
> }
> 
> How to do? Is there any alternatives? Many thanks!
> 
> Best,
> Jinsong
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value to a specific position of a list

2017-04-30 Thread Jinsong Zhao

On 2017/4/30 23:17, Jinsong Zhao wrote:

Hi there,

I have a problem with assign(). Here is the demo code:

for (i in 1:10) {
   # create a list with variable name as list_1, list_2, ..., etc.
   assign(paste("list_", i, sep = ""), list())
   # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
   # list_1[[1]] <- 5 # works, however
   assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work

 # wrong code in previous message, the correct on should be:
 assign(paste("list_", i, "[[1]]", sep = ""), 5) # does not work...

}

How to do? Is there any alternatives? Many thanks!

Best,
Jinsong


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to assign a value to a specific position of a list

2017-04-30 Thread Jinsong Zhao

Hi there,

I have a problem with assign(). Here is the demo code:

for (i in 1:10) {
   # create a list with variable name as list_1, list_2, ..., etc.
   assign(paste("list_", i, sep = ""), list())
   # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
   # list_1[[1]] <- 5 # works, however
   assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
}

How to do? Is there any alternatives? Many thanks!

Best,
Jinsong

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to assign height value on bar plot?

2012-05-27 Thread Greg Snow
Did you look at the help page that Uwe directed you to?  At least one
of the examples on that page demonstrates adding text to a barplot.
But before you do that you should read through the discussion here:
http://tolstoy.newcastle.edu.au/R/e2/help/07/08/22858.html on why you
might not want to add text to the top of the bars and some of the
alternatives.

On Sat, May 26, 2012 at 12:30 PM, Manish Gupta
mandecent.gu...@gmail.com wrote:
 My Question is how to write height length on each bar.

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/How-to-assign-height-value-on-bar-plot-tp4631457p4631468.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to assign height value on bar plot?

2012-05-26 Thread Manish Gupta
Hi,

How to assign height value on bar plot for each bar?

http://r.789695.n4.nabble.com/file/n4631457/Barplot.png 

--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-assign-height-value-on-bar-plot-tp4631457.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to assign height value on bar plot?

2012-05-26 Thread Uwe Ligges



On 26.05.2012 18:07, Manish Gupta wrote:

Hi,

How to assign height value on bar plot for each bar?


See ?barplot.

Uwe Ligges


http://r.789695.n4.nabble.com/file/n4631457/Barplot.png

--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-assign-height-value-on-bar-plot-tp4631457.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to assign height value on bar plot?

2012-05-26 Thread Manish Gupta
My Question is how to write height length on each bar.

--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-assign-height-value-on-bar-plot-tp4631457p4631468.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to assign height value on bar plot?

2012-05-26 Thread Jim Lemon

On 05/27/2012 04:30 AM, Manish Gupta wrote:

My Question is how to write height length on each bar.


Hi Manish,
If don't have stacked bars, it is fairly simple:

barpos-barplot(height,...)
text(barpos,height,labels=height)

usually with a bit of fooling around with the cex argument. If you want 
each value in a little box, try boxed.labels in plotrix.


Jim

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to assign a value?

2011-12-11 Thread Jinsong Zhao

Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
   assign(paste(a., i, sep = ), 1:i)
   get(paste(a., i, sep = ))[i] - i+50
}

I get the following error message:

Error in get(paste(a., i, sep = ))[i] - i + 50 :
  target of assignment expands to non-language object

I have read the FAQ How can I turn a string into a variable?, however, 
I don't find a way to deal with:


get(paste(a., i, sep = ))[i] - i+50

Any suggestions? Thanks in advance!

Regards,
Jinsong

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread David Winsemius


On Dec 11, 2011, at 10:27 AM, Jinsong Zhao wrote:


Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
  assign(paste(a., i, sep = ), 1:i)
  get(paste(a., i, sep = ))[i] - i+50
}


Just one matrix? Then you seem to have inappropriately borrowed using  
. as an indexing operation. In R that is just another character when  
used as an object name. a.1 is notgoing to evaulate to  a[1].  Look  
at what you would have had after


 for (i in 1:9) {
+   assign(paste(a., i, sep = ), 1:i)
+   }
 ls()
 [1] aa.1  a.2
 [4] a.3  a.4  a.5
 [7] a.6  a.7  a.8
[10] a.9

 a.1
[1] 1
 a.2
[1] 1 2

Each of those assign() operations created a single vector of length i.  
I doubt that was what you intended,


Better would be to describe your objects and your intentions, rather  
than expecting us to understand your goals by just looking at code  
that doesn't achieve thos goals. (There is no `get-` function which  
was the source of the  error.)





I get the following error message:

Error in get(paste(a., i, sep = ))[i] - i + 50 :
 target of assignment expands to non-language object

I have read the FAQ How can I turn a string into a variable?,  
however, I don't find a way to deal with:


get(paste(a., i, sep = ))[i] - i+50

Any suggestions? Thanks in advance!

Regards,
Jinsong

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread Patrick Burns

You are basically in R Inferno Circle 8.1.40.

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

On 11/12/2011 15:27, Jinsong Zhao wrote:

Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
assign(paste(a., i, sep = ), 1:i)
get(paste(a., i, sep = ))[i] - i+50
}

I get the following error message:

Error in get(paste(a., i, sep = ))[i] - i + 50 :
target of assignment expands to non-language object

I have read the FAQ How can I turn a string into a variable?, however,
I don't find a way to deal with:

get(paste(a., i, sep = ))[i] - i+50

Any suggestions? Thanks in advance!

Regards,
Jinsong

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread William Dunlap
I find that get() and assign() are awkward to use
and that the syntax is easier if you put your objects into
a list or environment.  To me, it also makes it
clearer what the code is doing and keeps the output
of objects() shorter and easier to manage.  E.g.,

nResults - 9
results - vector(list, nResults) # or results - new.env()
for(i in 1:nResults) {
   resultName - paste(a., i, sep=)
   results[[resultName]] - 1:i
   results[[resultName]][i] - i+50
}

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf Of Jinsong Zhao
 Sent: Sunday, December 11, 2011 7:28 AM
 To: r-help@r-project.org
 Subject: [R] how to assign a value?
 
 Hi there,
 
 I hope to modify values in a vector or matrix in the following code:
 
 for (i in 1:9) {
 assign(paste(a., i, sep = ), 1:i)
 get(paste(a., i, sep = ))[i] - i+50
 }
 
 I get the following error message:
 
 Error in get(paste(a., i, sep = ))[i] - i + 50 :
target of assignment expands to non-language object
 
 I have read the FAQ How can I turn a string into a variable?, however,
 I don't find a way to deal with:
 
 get(paste(a., i, sep = ))[i] - i+50
 
 Any suggestions? Thanks in advance!
 
 Regards,
 Jinsong
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread Jinsong Zhao

On 2011-12-12 0:00, David Winsemius wrote:


On Dec 11, 2011, at 10:27 AM, Jinsong Zhao wrote:


Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
assign(paste(a., i, sep = ), 1:i)
get(paste(a., i, sep = ))[i] - i+50
}


Just one matrix? Then you seem to have inappropriately borrowed using
. as an indexing operation. In R that is just another character when
used as an object name. a.1 is notgoing to evaulate to a[1]. Look at
what you would have had after

  for (i in 1:9) {
+ assign(paste(a., i, sep = ), 1:i)
+ }
  ls()
[1] a a.1 a.2
[4] a.3 a.4 a.5
[7] a.6 a.7 a.8
[10] a.9

  a.1
[1] 1
  a.2
[1] 1 2

Each of those assign() operations created a single vector of length i. I
doubt that was what you intended,


yes, it was what I intended.



Better would be to describe your objects and your intentions, rather
than expecting us to understand your goals by just looking at code that
doesn't achieve thos goals. (There is no `get-` function which was the
source of the error.)



The question is why

get(paste(a., i, sep = ))[i] - i+50

give the following error message:

Error in get(paste(a., i, sep = ))[i] - i + 50 :
  target of assignment expands to non-language object

The a.1 to  a.9 was created in the previous step.

if only

get(paste(a., i, sep = ))[i]

can give correct output. Why I cannot assign values to it?


Regards,
Jinsong

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread Jinsong Zhao

On 2011-12-12 1:16, Patrick Burns wrote:

You are basically in R Inferno Circle 8.1.40.

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf



Thanks.

R_inferno is a good material for me. In this document, there is several 
sections titled string not the name. I try try to change


get(paste(a., i, sep = ))[i] - i+50

to

assign(get(paste(a., i, sep = ))[i], i+50)

however, error message:

Error in assign(get(paste(a., i, sep = ))[i], i + 50) :
  invalid first argument

I don't know why...

Regards,
Jinsong



On 11/12/2011 15:27, Jinsong Zhao wrote:

Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
assign(paste(a., i, sep = ), 1:i)
get(paste(a., i, sep = ))[i] - i+50
}

I get the following error message:

Error in get(paste(a., i, sep = ))[i] - i + 50 :
target of assignment expands to non-language object

I have read the FAQ How can I turn a string into a variable?, however,
I don't find a way to deal with:

get(paste(a., i, sep = ))[i] - i+50

Any suggestions? Thanks in advance!

Regards,
Jinsong



__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread David Winsemius


On Dec 11, 2011, at 9:07 PM, Jinsong Zhao wrote:


On 2011-12-12 0:00, David Winsemius wrote:


On Dec 11, 2011, at 10:27 AM, Jinsong Zhao wrote:


Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
assign(paste(a., i, sep = ), 1:i)
get(paste(a., i, sep = ))[i] - i+50
}


Just one matrix? Then you seem to have inappropriately borrowed using
. as an indexing operation. In R that is just another character  
when
used as an object name. a.1 is notgoing to evaulate to a[1]. Look  
at

what you would have had after

 for (i in 1:9) {
+ assign(paste(a., i, sep = ), 1:i)
+ }
 ls()
[1] a a.1 a.2
[4] a.3 a.4 a.5
[7] a.6 a.7 a.8
[10] a.9

 a.1
[1] 1
 a.2
[1] 1 2

Each of those assign() operations created a single vector of length  
i. I

doubt that was what you intended,


yes, it was what I intended.


Then you are free to continue banging your head against a wall.





Better would be to describe your objects and your intentions, rather
than expecting us to understand your goals by just looking at code  
that
doesn't achieve thos goals. (There is no `get-` function which was  
the

source of the error.)



The question is why

get(paste(a., i, sep = ))[i] - i+50

give the following error message:


What part of THERE IS NO get- function (much less a `get[-`  
function)  don't you understand?




Error in get(paste(a., i, sep = ))[i] - i + 50 :
 target of assignment expands to non-language object

The a.1 to  a.9 was created in the previous step.

if only

get(paste(a., i, sep = ))[i]

can give correct output.


Right. They are there and can even be indexed:

 get(paste(a, 9, sep=.))[9]
[1] 9

You could assign the value of get(paste(a, 9, sep=.)) to an  
intermediate object, which you could then reference using [ and then  
use `assign` to push that object's value back to an object named a. 
1, , a.2, etc. Very clumsy and not an idiom that people want to  
promote.


 x - get(paste(a, 9, sep=.))
 x[9] - x[9]+50
 assign(paste(a, 9, sep=.), x)
 a.9
[1]  1  2  3  4  5  6  7  8 59



Why I cannot assign values to it?


Using get, you mean? Because that is not the way R is designed. get()  
returns a value. `assign` is used... wait for it ... assignment.


 get(paste(a, 1, sep=.))
[1] 1

Not a.1 but rather a.1's value. You cannot assign something else to  
the number 1. You are free to complain about the fact that R is is not  
languageX as much as you like, but it won't create new capabilities  
for functions. You've been given advice about how to get to the goal  
you desire by both Dunlap and Burns. The counter-question is why you  
have such trouble accepting advice.




Regards,
Jinsong


David Winsemius, MD
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to assign a value?

2011-12-11 Thread Jinsong Zhao

On 2011-12-12 10:58, David Winsemius wrote:


On Dec 11, 2011, at 9:07 PM, Jinsong Zhao wrote:


On 2011-12-12 0:00, David Winsemius wrote:


On Dec 11, 2011, at 10:27 AM, Jinsong Zhao wrote:


Hi there,

I hope to modify values in a vector or matrix in the following code:

for (i in 1:9) {
assign(paste(a., i, sep = ), 1:i)
get(paste(a., i, sep = ))[i] - i+50
}


Just one matrix? Then you seem to have inappropriately borrowed using
. as an indexing operation. In R that is just another character when
used as an object name. a.1 is notgoing to evaulate to a[1]. Look at
what you would have had after

 for (i in 1:9) {
+ assign(paste(a., i, sep = ), 1:i)
+ }
 ls()
[1] a a.1 a.2
[4] a.3 a.4 a.5
[7] a.6 a.7 a.8
[10] a.9

 a.1
[1] 1
 a.2
[1] 1 2

Each of those assign() operations created a single vector of length i. I
doubt that was what you intended,


yes, it was what I intended.


Then you are free to continue banging your head against a wall.





Better would be to describe your objects and your intentions, rather
than expecting us to understand your goals by just looking at code that
doesn't achieve thos goals. (There is no `get-` function which was the
source of the error.)



The question is why

get(paste(a., i, sep = ))[i] - i+50

give the following error message:


What part of THERE IS NO get- function (much less a `get[-`
function) don't you understand?


Sorry, I didn't understand it in the previous post. Now, it seems clear...





Error in get(paste(a., i, sep = ))[i] - i + 50 :
target of assignment expands to non-language object

The a.1 to a.9 was created in the previous step.

if only

get(paste(a., i, sep = ))[i]

can give correct output.


Right. They are there and can even be indexed:

  get(paste(a, 9, sep=.))[9]
[1] 9

You could assign the value of get(paste(a, 9, sep=.)) to an
intermediate object, which you could then reference using [ and then
use `assign` to push that object's value back to an object named a.1,
, a.2, etc. Very clumsy and not an idiom that people want to promote.

  x - get(paste(a, 9, sep=.))
  x[9] - x[9]+50
  assign(paste(a, 9, sep=.), x)
  a.9
[1] 1 2 3 4 5 6 7 8 59


Yes, the intermediate object could be used to archive my goal:

for (i in 1:9) {
   assign(paste(a, i, sep = .), 1:i)
   x - get(paste(a, i, sep = .))
   x[[i]] - x[[i]] + 50
   assign(paste(a, i, sep = .), x)
}





Why I cannot assign values to it?


Using get, you mean? Because that is not the way R is designed. get()
returns a value. `assign` is used... wait for it ... assignment.

  get(paste(a, 1, sep=.))
[1] 1

Not a.1 but rather a.1's value. You cannot assign something else to the
number 1. You are free to complain about the fact that R is is not
languageX as much as you like, but it won't create new capabilities for
functions. You've been given advice about how to get to the goal you
desire by both Dunlap and Burns. The counter-question is why you have
such trouble accepting advice.



I don't have trouble accepting advice. I am just curious about the 
error. Thank you very much for your patience.




Regards,
Jinsong


David Winsemius, MD
West Hartford, CT




Regards,
Jinsong

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to assign vector value as object name

2011-08-23 Thread J. Augusiak
Dear list,

 

I have a table with entries for 20 animals - x and y coordinates to analyze
movement with the package adehabitat.

 

The package does all the necessary analyses, but I need to create an object
of class ltraj for each animal first. For this kind of object I need to
define xy coordinates, id, and few other things for each individual.

 

Now I don't want to run a code 20 times but wanted to include for-loops to
have it all run automatically on my entire table.

 

First, create a vector with the variable names (say xy.1, xy.2,..., xy.20),
then extract the value xy.1 and assign a set of coordinates for animal 1,
etc.). When I have done this for all variables required to form an ltraj
object, I want to create all 20 objects for all 20 animals, by only
referring to my vector with variable names with the idea that the actual
values behind make it into the object.

 

With this piece of code, I was able to extract xy.1 as name for a data
frame containing the coordinates for animal 1:

 

nam_xy - paste(xy., 1, sep=)
# creates xy.1

coord.x - paste(XCM, 1, sep=)

coord.y - paste(YCM, 1, sep=)

for (i in 2:20) {

nam_xy[i] - paste(xy., i, sep=)

coord.x[i] - paste(XCM, i, sep=)

coord.y[i] - paste(YCM, i, sep=)
# creates xy.2, xy.3, etc.

}

for (k in 1:20) {

  assign(nam_xy[[k]],data.mov[,c(coord.x[k],coord.y[k])])# takes
xy.1 as name for a date frame, xy.2 for the next, etc.

  }

 

With this piece of code however, I get error messages. Apparently, the
date/time that I defined (one for each row, same timeline for all 20
animals) and nrow(xy) do not have the same length, although when I check it
manually length(date) is equal to nrow(xy.1). So it seems to me that I have
problems with extracting the right information, that is that when I want to
extract xy.1 in the last for-loop, it doesn't automatically give the data
frame of x-y- coordinates that I assigned to it earlier.:

 

nam_ltraj - paste(tr1.,1,sep=) # set up
vector of names for ltraj objects

for (i in 2:20) {

nam_ltraj[i] - paste(tr1.,i,sep=)   # extends
vector of names 

}

for (k in 1:20) {

assign(nam_ltraj[k], as.ltraj(xy=nam_xy[[k]], date=data.mov$DATE,
# assign an object name to an ltraj object with animal specific data

id=nam_id[[k]], burst=nam_burst[[k]], typeII=T,
slsp=c(remove,missing)))

}

 

Any input on how to solve this is highly appreciated!

 

Jacqueline

 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.