Dynamic reference, right-hand side of function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamic reference, right-hand side of function

Love Bohman
Hi R-users!
Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.

I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
aa_2000 <- as.numeric(aa_2000[,1])
However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.

I would very much appreciate some help with this issue!
All the best,
Love

year <- 2002
dataname <- paste0("aa_",year)
assign(paste0(dataname), as.numeric(aa_2002[,1]))

year <- 2003
assign(paste0("aa_",year), as.numeric(aa_2003))

year <- 2005
assign(paste0("aa_",year), aa_2005[,1])

list1 <- c(2000:2007)
list1[c(7)]
assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Ek Esawi
Hi Love,

I am not sure if I understand your question and it will help if you
provided a sample data frame, sample of your code and sample of your output
and the output you desire. Having said that, I think you could use cbind to
join all datasets into a matrix and use the apply family of functions to
achieve what you are after.

On Mon, Dec 4, 2017 at 6:33 AM, Love Bohman <[hidden email]>
wrote:

> Hi R-users!
> Being new to R, and a fairly advanced Stata-user, I guess part of my
> problem is that my mindset (and probably my language as well) is wrong.
> Anyway, I have what I guess is a rather simple problem, that I now without
> success spent days trying to solve.
>
> I have a bunch of datasets imported from Stata that is labelled aa_2000
> aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of
> one column only. The columns consists of integer numbers. I need to convert
> the data to vectors, which I found several ways to do. I use, for example:
> aa_2000 <- as.numeric(aa_2000[,1])
> However, when trying to automate the task, so I don't have to write a line
> of code for each dataset, I get stuck. Since I'm a Stata user, my first
> attempt is trying to make a loop in order to loop over all datasets.
> However, I manage to write a loop that works for the left-hand side of the
> syntax, but not for the right-hand side.
> I have included some examples from my struggles to solve the issue below,
> what they all have in common is that I don't manage to call for any "macro"
> (is that only a Stata-word?) in the right hand side of the functions. When
> I try to replace the static reference with a dynamic one (like in the
> left-hand side), the syntax just doesn't work.
>
> I would very much appreciate some help with this issue!
> All the best,
> Love
>
> year <- 2002
> dataname <- paste0("aa_",year)
> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>
> year <- 2003
> assign(paste0("aa_",year), as.numeric(aa_2003))
>
> year <- 2005
> assign(paste0("aa_",year), aa_2005[,1])
>
> list1 <- c(2000:2007)
> list1[c(7)]
> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Peter Dalgaard-2
In reply to this post by Love Bohman
The generic rule is that R is not a macro language, so looping of names of things gets awkward. It is usually easier to use compound objects like lists and iterate over them. E.g.

datanames <- paste0("aa_", 2000:2007)
datalist <- lapply(datanames, get)
names(datalist) <- datanames
col1 <- lapply(datalist, "[[", 1)
colnum <- lapply(col1, as.numeric)

(The 2nd line assumes that the damage has already been done so that you have aa_2000 ... aa_2007 in your workspace. You might alternatively create the list directly while importing the data.)

-pd

> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]> wrote:
>
> Hi R-users!
> Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.
>
> I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
> aa_2000 <- as.numeric(aa_2000[,1])
> However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
> I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.
>
> I would very much appreciate some help with this issue!
> All the best,
> Love
>
> year <- 2002
> dataname <- paste0("aa_",year)
> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>
> year <- 2003
> assign(paste0("aa_",year), as.numeric(aa_2003))
>
> year <- 2005
> assign(paste0("aa_",year), aa_2005[,1])
>
> list1 <- c(2000:2007)
> list1[c(7)]
> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Love Bohman
Hi!
Thanks for the replies!
I understand people more accustomed to R doesn't like looping much, and that thinking about loops is something I do since I worked with Stata a lot. The syntax from Peter Dalgaard was really clever, and I learned a lot from it, even though it didn't solve my problem (I guess it wasn't very well explained). My problem was basically that I have a data matrix consisting of just 1 row, and I want to convert that row into a vector. However, when trying to do that dynamically, I couldn't get R to read the right hand side of the syntax as a variable name instead of a string. However, together with a colleague I finally solved it with the (eval(as.name()) function (I include the loop I used below). I understand that looping isn't kosher among you more devoted R-users, and eventually I hope I will learn to use lists in the future instead.

Thanks!
Love


for (year in 2000:2007){
varname <- paste0("aa_",year)
assign(paste0(varname), as.vector(eval(as.name(varname))))
}

-----Ursprungligt meddelande-----
Från: peter dalgaard [mailto:[hidden email]]
Skickat: den 4 december 2017 16:39
Till: Love Bohman <[hidden email]>
Kopia: [hidden email]
Ämne: Re: [R] Dynamic reference, right-hand side of function

The generic rule is that R is not a macro language, so looping of names of things gets awkward. It is usually easier to use compound objects like lists and iterate over them. E.g.

datanames <- paste0("aa_", 2000:2007)
datalist <- lapply(datanames, get)
names(datalist) <- datanames
col1 <- lapply(datalist, "[[", 1)
colnum <- lapply(col1, as.numeric)

(The 2nd line assumes that the damage has already been done so that you have aa_2000 ... aa_2007 in your workspace. You might alternatively create the list directly while importing the data.)

-pd

> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]> wrote:
>
> Hi R-users!
> Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.
>
> I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
> aa_2000 <- as.numeric(aa_2000[,1])
> However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
> I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.
>
> I would very much appreciate some help with this issue!
> All the best,
> Love
>
> year <- 2002
> dataname <- paste0("aa_",year)
> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>
> year <- 2003
> assign(paste0("aa_",year), as.numeric(aa_2003))
>
> year <- 2005
> assign(paste0("aa_",year), aa_2005[,1])
>
> list1 <- c(2000:2007)
> list1[c(7)]
> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Peter Dalgaard-2
Um, if you insist on doing it that way, at least use

assign(varname, as.vector(get(varname)))

-pd

> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]> wrote:
>
> Hi!
> Thanks for the replies!
> I understand people more accustomed to R doesn't like looping much, and that thinking about loops is something I do since I worked with Stata a lot. The syntax from Peter Dalgaard was really clever, and I learned a lot from it, even though it didn't solve my problem (I guess it wasn't very well explained). My problem was basically that I have a data matrix consisting of just 1 row, and I want to convert that row into a vector. However, when trying to do that dynamically, I couldn't get R to read the right hand side of the syntax as a variable name instead of a string. However, together with a colleague I finally solved it with the (eval(as.name()) function (I include the loop I used below). I understand that looping isn't kosher among you more devoted R-users, and eventually I hope I will learn to use lists in the future instead.
>
> Thanks!
> Love
>
>
> for (year in 2000:2007){
> varname <- paste0("aa_",year)
> assign(paste0(varname), as.vector(eval(as.name(varname))))
> }
>
> -----Ursprungligt meddelande-----
> Från: peter dalgaard [mailto:[hidden email]]
> Skickat: den 4 december 2017 16:39
> Till: Love Bohman <[hidden email]>
> Kopia: [hidden email]
> Ämne: Re: [R] Dynamic reference, right-hand side of function
>
> The generic rule is that R is not a macro language, so looping of names of things gets awkward. It is usually easier to use compound objects like lists and iterate over them. E.g.
>
> datanames <- paste0("aa_", 2000:2007)
> datalist <- lapply(datanames, get)
> names(datalist) <- datanames
> col1 <- lapply(datalist, "[[", 1)
> colnum <- lapply(col1, as.numeric)
>
> (The 2nd line assumes that the damage has already been done so that you have aa_2000 ... aa_2007 in your workspace. You might alternatively create the list directly while importing the data.)
>
> -pd
>
>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]> wrote:
>>
>> Hi R-users!
>> Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.
>>
>> I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
>> aa_2000 <- as.numeric(aa_2000[,1])
>> However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
>> I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.
>>
>> I would very much appreciate some help with this issue!
>> All the best,
>> Love
>>
>> year <- 2002
>> dataname <- paste0("aa_",year)
>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>>
>> year <- 2003
>> assign(paste0("aa_",year), as.numeric(aa_2003))
>>
>> year <- 2005
>> assign(paste0("aa_",year), aa_2005[,1])
>>
>> list1 <- c(2000:2007)
>> list1[c(7)]
>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Love Bohman
:-)
I don't insist on anything, I'm just struggling to learn a new language and partly a new way of thinking, and I really appreciate the corrections. I hope I someday will be able to handle lists in R as easy as I handle loops in Stata...
Thanks again!

Love
 

-----Ursprungligt meddelande-----
Från: peter dalgaard [mailto:[hidden email]]
Skickat: den 4 december 2017 23:09
Till: Love Bohman <[hidden email]>
Kopia: [hidden email]
Ämne: Re: [R] Dynamic reference, right-hand side of function

Um, if you insist on doing it that way, at least use

assign(varname, as.vector(get(varname)))

-pd

> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]> wrote:
>
> Hi!
> Thanks for the replies!
> I understand people more accustomed to R doesn't like looping much, and that thinking about loops is something I do since I worked with Stata a lot. The syntax from Peter Dalgaard was really clever, and I learned a lot from it, even though it didn't solve my problem (I guess it wasn't very well explained). My problem was basically that I have a data matrix consisting of just 1 row, and I want to convert that row into a vector. However, when trying to do that dynamically, I couldn't get R to read the right hand side of the syntax as a variable name instead of a string. However, together with a colleague I finally solved it with the (eval(as.name()) function (I include the loop I used below). I understand that looping isn't kosher among you more devoted R-users, and eventually I hope I will learn to use lists in the future instead.
>
> Thanks!
> Love
>
>
> for (year in 2000:2007){
> varname <- paste0("aa_",year)
> assign(paste0(varname), as.vector(eval(as.name(varname))))
> }
>
> -----Ursprungligt meddelande-----
> Från: peter dalgaard [mailto:[hidden email]]
> Skickat: den 4 december 2017 16:39
> Till: Love Bohman <[hidden email]>
> Kopia: [hidden email]
> Ämne: Re: [R] Dynamic reference, right-hand side of function
>
> The generic rule is that R is not a macro language, so looping of names of things gets awkward. It is usually easier to use compound objects like lists and iterate over them. E.g.
>
> datanames <- paste0("aa_", 2000:2007)
> datalist <- lapply(datanames, get)
> names(datalist) <- datanames
> col1 <- lapply(datalist, "[[", 1)
> colnum <- lapply(col1, as.numeric)
>
> (The 2nd line assumes that the damage has already been done so that
> you have aa_2000 ... aa_2007 in your workspace. You might
> alternatively create the list directly while importing the data.)
>
> -pd
>
>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]> wrote:
>>
>> Hi R-users!
>> Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.
>>
>> I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
>> aa_2000 <- as.numeric(aa_2000[,1])
>> However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
>> I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.
>>
>> I would very much appreciate some help with this issue!
>> All the best,
>> Love
>>
>> year <- 2002
>> dataname <- paste0("aa_",year)
>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>>
>> year <- 2003
>> assign(paste0("aa_",year), as.numeric(aa_2003))
>>
>> year <- 2005
>> assign(paste0("aa_",year), aa_2005[,1])
>>
>> list1 <- c(2000:2007)
>> list1[c(7)]
>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]









______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

R help mailing list-2
In reply to this post by Love Bohman
Note that in
    for (year in 2000:2007){
        varname <- paste0("aa_",year)
        assign(paste0(varname), as.vector(eval(as.name(varname))))
    }
the paste0(varname) is redundant - varname was just computed as the return
value of paste0().

People are trying to steer you towards making a list or environment that
contains
only your aa_XXXX objects because that will make subsequent code more
readable and maintainable.  (The initial setup can be weird because of the
string manipulations required.)

E.g., suppose you have datasets aa_XXXX for some arbitrary set of years.  If
you did not create a list of them when reading in the data
    aa_1970 <- scan(quiet=TRUE, text="2 3 5 8 10")
    aa_1981 <- scan(quiet=TRUE, text="11 17 18 19 23")
then you can package all of them in the current environment into a list with
    aa_names <- objects(pattern="^aa_[[:digit:]]{4}$")
    names(aa_names) <- sub("^aa_", "", aa_names) # just the year
    aa_list <- lapply(aa_names, get)
Once you have a list of all your datasets then you can do operations on all
the
elements of the list without having to remember which years you have data
for.
    aa_list <- lapply(aa_list, as.numeric)
    aa_trends <- lapply(aa_list, function(x) { iota <- seq_along(x) ;
coef(lm(x ~ iota)) })

To read in data from a bunch of files called aa_XXXX.txt, making the names
of
the list be the XXXX part of the file name, do
    aa_files <- dir(pattern="^aa_[[:digit:]]{4}\\.txt$")
    names(aa_files) <- sub("aa_([[:digit:]]{4})\\.txt$", "\\1", aa_files)
    aa_list <- lapply(aa_files, readAnAAFile)
where 'readAnAAFile' is the function you use to read one such file.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 4, 2017 at 1:46 PM, Love Bohman <[hidden email]>
wrote:

> Hi!
> Thanks for the replies!
> I understand people more accustomed to R doesn't like looping much, and
> that thinking about loops is something I do since I worked with Stata a
> lot. The syntax from Peter Dalgaard was really clever, and I learned a lot
> from it, even though it didn't solve my problem (I guess it wasn't very
> well explained). My problem was basically that I have a data matrix
> consisting of just 1 row, and I want to convert that row into a vector.
> However, when trying to do that dynamically, I couldn't get R to read the
> right hand side of the syntax as a variable name instead of a string.
> However, together with a colleague I finally solved it with the (eval(
> as.name()) function (I include the loop I used below). I understand that
> looping isn't kosher among you more devoted R-users, and eventually I hope
> I will learn to use lists in the future instead.
>
> Thanks!
> Love
>
>
> for (year in 2000:2007){
> varname <- paste0("aa_",year)
> assign(paste0(varname), as.vector(eval(as.name(varname))))
> }
>
> -----Ursprungligt meddelande-----
> Från: peter dalgaard [mailto:[hidden email]]
> Skickat: den 4 december 2017 16:39
> Till: Love Bohman <[hidden email]>
> Kopia: [hidden email]
> Ämne: Re: [R] Dynamic reference, right-hand side of function
>
> The generic rule is that R is not a macro language, so looping of names of
> things gets awkward. It is usually easier to use compound objects like
> lists and iterate over them. E.g.
>
> datanames <- paste0("aa_", 2000:2007)
> datalist <- lapply(datanames, get)
> names(datalist) <- datanames
> col1 <- lapply(datalist, "[[", 1)
> colnum <- lapply(col1, as.numeric)
>
> (The 2nd line assumes that the damage has already been done so that you
> have aa_2000 ... aa_2007 in your workspace. You might alternatively create
> the list directly while importing the data.)
>
> -pd
>
> > On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]>
> wrote:
> >
> > Hi R-users!
> > Being new to R, and a fairly advanced Stata-user, I guess part of my
> problem is that my mindset (and probably my language as well) is wrong.
> Anyway, I have what I guess is a rather simple problem, that I now without
> success spent days trying to solve.
> >
> > I have a bunch of datasets imported from Stata that is labelled aa_2000
> aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of
> one column only. The columns consists of integer numbers. I need to convert
> the data to vectors, which I found several ways to do. I use, for example:
> > aa_2000 <- as.numeric(aa_2000[,1])
> > However, when trying to automate the task, so I don't have to write a
> line of code for each dataset, I get stuck. Since I'm a Stata user, my
> first attempt is trying to make a loop in order to loop over all datasets.
> However, I manage to write a loop that works for the left-hand side of the
> syntax, but not for the right-hand side.
> > I have included some examples from my struggles to solve the issue
> below, what they all have in common is that I don't manage to call for any
> "macro" (is that only a Stata-word?) in the right hand side of the
> functions. When I try to replace the static reference with a dynamic one
> (like in the left-hand side), the syntax just doesn't work.
> >
> > I would very much appreciate some help with this issue!
> > All the best,
> > Love
> >
> > year <- 2002
> > dataname <- paste0("aa_",year)
> > assign(paste0(dataname), as.numeric(aa_2002[,1]))
> >
> > year <- 2003
> > assign(paste0("aa_",year), as.numeric(aa_2003))
> >
> > year <- 2005
> > assign(paste0("aa_",year), aa_2005[,1])
> >
> > list1 <- c(2000:2007)
> > list1[c(7)]
> > assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
> >
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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: [hidden email]  Priv: [hidden email]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Jeff Newmiller
In reply to this post by Love Bohman
Loops are not evil, and no-one in this thread said they are. But I
believe your failure to provide a reproducible example is creating
confusion, since you may be using words that mean one thing to you and
something else to the readers here.

################################
# A reproducible example includes a tiny set of sample data
# Since we cannot reproducibly refer to filenames (your directories
# and files in them are unlikely to be like mine), I will
# use a little trick to read from data in the example:

dta <- read.csv( text=
"1.1,3.0,5,7.4,4,2.2,0
", header=FALSE)
str(dta)
#> 'data.frame':    1 obs. of  7 variables:
#>  $ V1: num 1.1
#>  $ V2: num 3
#>  $ V3: int 5
#>  $ V4: num 7.4
#>  $ V5: int 4
#>  $ V6: num 2.2
#>  $ V7: int 0

# note that I did not use "data" as the name because
# there is a commonly-used function by that name in R
# that could be confused with your variable

# if you have your object already in memory, you can use the
# dput function to create R code that will re-create it in our
# working environments.

dput( dta )
#> structure(list(V1 = 1.1, V2 = 3, V3 = 5L, V4 = 7.4, V5 = 4L,
#>     V6 = 2.2, V7 = 0L), .Names = c("V1", "V2", "V3", "V4", "V5",
#> "V6", "V7"), class = "data.frame", row.names = c(NA, -1L))

# which you can put a variable name in front of in your example code:

dtasample <- structure(list( V1 = 1.1, V2 = 3, V3 = 5L
, V4 = 7.4, V5 = 4L, V6 = 2.2, V7 = 0L ) , .Names = c( "V1"
, "V2", "V3", "V4", "V5", "V6", "V7" ), class = "data.frame"
, row.names = c(NA, -1L) )

# and starting with that line you can make a self-contained
# (reproducible) example for us to investigate your problem with

# Note that reading a single row of data into R usually gets
# a data frame, which looks like a matrix but is not a matrix.
# Read the Introduction to R about these two types carefully.
# Each column in a data frame can have a different type of data,
# but in a vector or a matrix all rows and columns must be of
# the same type.

dtam <- as.matrix( dta )

# If you have any values that R cannot clearly identify as numeric
# or integer, then the next most general type of variable is
# character... and that is often something that trips up newbies,
# though I have no evidence that you have any non-numeric columns
# in your data frames.

dtax <- as.vector( dta )
str(dtax)
#> 'data.frame':    1 obs. of  7 variables:
#>  $ V1: num 1.1
#>  $ V2: num 3
#>  $ V3: int 5
#>  $ V4: num 7.4
#>  $ V5: int 4
#>  $ V6: num 2.2
#>  $ V7: int 0

# This actually makes no change to dta, because a data frame is already
# a list of columns, and lists are just vectors that can hold different
# types of variables, so dta is already a kind of vector.

dtan <- as.numeric( dta )
str(dtan)
#>  num [1:7] 1.1 3 5 7.4 4 2.2 0

# I suspect this is what you are trying to accomplish... but really,
# if we had an example of the data you are working with, we would
# already know.
################################

Some more explanations of reproducibility [1][2][3]

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html (read the
vignette)


On Mon, 4 Dec 2017, Love Bohman wrote:

> :-)
> I don't insist on anything, I'm just struggling to learn a new language and partly a new way of thinking, and I really appreciate the corrections. I hope I someday will be able to handle lists in R as easy as I handle loops in Stata...
> Thanks again!
>
> Love
>
>
> -----Ursprungligt meddelande-----
> Fr?n: peter dalgaard [mailto:[hidden email]]
> Skickat: den 4 december 2017 23:09
> Till: Love Bohman <[hidden email]>
> Kopia: [hidden email]
> ?mne: Re: [R] Dynamic reference, right-hand side of function
>
> Um, if you insist on doing it that way, at least use
>
> assign(varname, as.vector(get(varname)))
>
> -pd
>
>> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]> wrote:
>>
>> Hi!
>> Thanks for the replies!
>> I understand people more accustomed to R doesn't like looping much, and that thinking about loops is something I do since I worked with Stata a lot. The syntax from Peter Dalgaard was really clever, and I learned a lot from it, even though it didn't solve my problem (I guess it wasn't very well explained). My problem was basically that I have a data matrix consisting of just 1 row, and I want to convert that row into a vector. However, when trying to do that dynamically, I couldn't get R to read the right hand side of the syntax as a variable name instead of a string. However, together with a colleague I finally solved it with the (eval(as.name()) function (I include the loop I used below). I understand that looping isn't kosher among you more devoted R-users, and eventually I hope I will learn to use lists in the future instead.
>>
>> Thanks!
>> Love
>>
>>
>> for (year in 2000:2007){
>> varname <- paste0("aa_",year)
>> assign(paste0(varname), as.vector(eval(as.name(varname))))
>> }
>>
>> -----Ursprungligt meddelande-----
>> Fr?n: peter dalgaard [mailto:[hidden email]]
>> Skickat: den 4 december 2017 16:39
>> Till: Love Bohman <[hidden email]>
>> Kopia: [hidden email]
>> ?mne: Re: [R] Dynamic reference, right-hand side of function
>>
>> The generic rule is that R is not a macro language, so looping of names of things gets awkward. It is usually easier to use compound objects like lists and iterate over them. E.g.
>>
>> datanames <- paste0("aa_", 2000:2007)
>> datalist <- lapply(datanames, get)
>> names(datalist) <- datanames
>> col1 <- lapply(datalist, "[[", 1)
>> colnum <- lapply(col1, as.numeric)
>>
>> (The 2nd line assumes that the damage has already been done so that
>> you have aa_2000 ... aa_2007 in your workspace. You might
>> alternatively create the list directly while importing the data.)
>>
>> -pd
>>
>>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]> wrote:
>>>
>>> Hi R-users!
>>> Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.
>>>
>>> I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
>>> aa_2000 <- as.numeric(aa_2000[,1])
>>> However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
>>> I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.
>>>
>>> I would very much appreciate some help with this issue!
>>> All the best,
>>> Love
>>>
>>> year <- 2002
>>> dataname <- paste0("aa_",year)
>>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>>>
>>> year <- 2003
>>> assign(paste0("aa_",year), as.numeric(aa_2003))
>>>
>>> year <- 2005
>>> assign(paste0("aa_",year), aa_2005[,1])
>>>
>>> list1 <- c(2000:2007)
>>> list1[c(7)]
>>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] 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: [hidden email]  Priv: [hidden email]
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>
> ______________________________________________
> [hidden email] 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 Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Love Bohman
Hi again!
I know you don't find loops evil (well, at least not diabolic :-) ). (After many hours googling I have realized that thinking about loops rather than lists is a newbie thing we Stata-users do, I just jokingly pointed it out). Anyway, I'm really happy that you try to teach me some R-manners. Since I still get questions about what the h**k I mean by my strange question, I sort it out with an example:

I had a number of matrices, named in a consecutive manner:

aa_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0", header=FALSE))
aa_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0", header=FALSE))
aa_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0", header=FALSE))

I needed them to be vectors, and they weren't:

is.vector(aa_2000)

I finally solved it with this loop (well, I admit I shaped up the last line thanks to William Dunlap):

for (year in 2000:2002){
varname <- paste0("aa_",year)
assign(varname, as.vector(eval(as.name(varname))))
}

The loop obviously solved the problem:

is.vector(aa_2000)                    

However, you have taught me that I should have solved it more elegant with a data list:
 
bb_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0", header=FALSE))
bb_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0", header=FALSE))
bb_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0", header=FALSE))

is.vector(bb_2000)                    

datanames <- paste0("bb_", 2000:2002)
datalist <- lapply(datanames, get)
is.vector(datalist[1])


I learned a lot of code today, and I really appreciate it! A million thanks!
My R-superpowers are, well, not as minuscule as when I woke up this morning.

All the best,
Love (or maybe LoveR, my future superhero name)



-----Ursprungligt meddelande-----
Från: Jeff Newmiller [mailto:[hidden email]]
Skickat: den 5 december 2017 00:01
Till: Love Bohman <[hidden email]>
Kopia: peter dalgaard <[hidden email]>; [hidden email]
Ämne: Re: [R] Dynamic reference, right-hand side of function

Loops are not evil, and no-one in this thread said they are. But I believe your failure to provide a reproducible example is creating confusion, since you may be using words that mean one thing to you and something else to the readers here.

################################
# A reproducible example includes a tiny set of sample data # Since we cannot reproducibly refer to filenames (your directories # and files in them are unlikely to be like mine), I will # use a little trick to read from data in the example:

dta <- read.csv( text=
"1.1,3.0,5,7.4,4,2.2,0
", header=FALSE)
str(dta)
#> 'data.frame':    1 obs. of  7 variables:
#>  $ V1: num 1.1
#>  $ V2: num 3
#>  $ V3: int 5
#>  $ V4: num 7.4
#>  $ V5: int 4
#>  $ V6: num 2.2
#>  $ V7: int 0

# note that I did not use "data" as the name because # there is a commonly-used function by that name in R # that could be confused with your variable

# if you have your object already in memory, you can use the # dput function to create R code that will re-create it in our # working environments.

dput( dta )
#> structure(list(V1 = 1.1, V2 = 3, V3 = 5L, V4 = 7.4, V5 = 4L,
#>     V6 = 2.2, V7 = 0L), .Names = c("V1", "V2", "V3", "V4", "V5",
#> "V6", "V7"), class = "data.frame", row.names = c(NA, -1L))

# which you can put a variable name in front of in your example code:

dtasample <- structure(list( V1 = 1.1, V2 = 3, V3 = 5L , V4 = 7.4, V5 = 4L, V6 = 2.2, V7 = 0L ) , .Names = c( "V1"
, "V2", "V3", "V4", "V5", "V6", "V7" ), class = "data.frame"
, row.names = c(NA, -1L) )

# and starting with that line you can make a self-contained # (reproducible) example for us to investigate your problem with

# Note that reading a single row of data into R usually gets # a data frame, which looks like a matrix but is not a matrix.
# Read the Introduction to R about these two types carefully.
# Each column in a data frame can have a different type of data, # but in a vector or a matrix all rows and columns must be of # the same type.

dtam <- as.matrix( dta )

# If you have any values that R cannot clearly identify as numeric # or integer, then the next most general type of variable is # character... and that is often something that trips up newbies, # though I have no evidence that you have any non-numeric columns # in your data frames.

dtax <- as.vector( dta )
str(dtax)
#> 'data.frame':    1 obs. of  7 variables:
#>  $ V1: num 1.1
#>  $ V2: num 3
#>  $ V3: int 5
#>  $ V4: num 7.4
#>  $ V5: int 4
#>  $ V6: num 2.2
#>  $ V7: int 0

# This actually makes no change to dta, because a data frame is already # a list of columns, and lists are just vectors that can hold different # types of variables, so dta is already a kind of vector.

dtan <- as.numeric( dta )
str(dtan)
#>  num [1:7] 1.1 3 5 7.4 4 2.2 0

# I suspect this is what you are trying to accomplish... but really, # if we had an example of the data you are working with, we would # already know.
################################

Some more explanations of reproducibility [1][2][3]

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html (read the
vignette)


On Mon, 4 Dec 2017, Love Bohman wrote:

> :-)
> I don't insist on anything, I'm just struggling to learn a new language and partly a new way of thinking, and I really appreciate the corrections. I hope I someday will be able to handle lists in R as easy as I handle loops in Stata...
> Thanks again!
>
> Love
>
>
> -----Ursprungligt meddelande-----
> Fr?n: peter dalgaard [mailto:[hidden email]]
> Skickat: den 4 december 2017 23:09
> Till: Love Bohman <[hidden email]>
> Kopia: [hidden email]
> ?mne: Re: [R] Dynamic reference, right-hand side of function
>
> Um, if you insist on doing it that way, at least use
>
> assign(varname, as.vector(get(varname)))
>
> -pd
>
>> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]> wrote:
>>
>> Hi!
>> Thanks for the replies!
>> I understand people more accustomed to R doesn't like looping much, and that thinking about loops is something I do since I worked with Stata a lot. The syntax from Peter Dalgaard was really clever, and I learned a lot from it, even though it didn't solve my problem (I guess it wasn't very well explained). My problem was basically that I have a data matrix consisting of just 1 row, and I want to convert that row into a vector. However, when trying to do that dynamically, I couldn't get R to read the right hand side of the syntax as a variable name instead of a string. However, together with a colleague I finally solved it with the (eval(as.name()) function (I include the loop I used below). I understand that looping isn't kosher among you more devoted R-users, and eventually I hope I will learn to use lists in the future instead.
>>
>> Thanks!
>> Love
>>
>>
>> for (year in 2000:2007){
>> varname <- paste0("aa_",year)
>> assign(paste0(varname), as.vector(eval(as.name(varname))))
>> }
>>
>> -----Ursprungligt meddelande-----
>> Fr?n: peter dalgaard [mailto:[hidden email]]
>> Skickat: den 4 december 2017 16:39
>> Till: Love Bohman <[hidden email]>
>> Kopia: [hidden email]
>> ?mne: Re: [R] Dynamic reference, right-hand side of function
>>
>> The generic rule is that R is not a macro language, so looping of names of things gets awkward. It is usually easier to use compound objects like lists and iterate over them. E.g.
>>
>> datanames <- paste0("aa_", 2000:2007)
>> datalist <- lapply(datanames, get)
>> names(datalist) <- datanames
>> col1 <- lapply(datalist, "[[", 1)
>> colnum <- lapply(col1, as.numeric)
>>
>> (The 2nd line assumes that the damage has already been done so that
>> you have aa_2000 ... aa_2007 in your workspace. You might
>> alternatively create the list directly while importing the data.)
>>
>> -pd
>>
>>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]> wrote:
>>>
>>> Hi R-users!
>>> Being new to R, and a fairly advanced Stata-user, I guess part of my problem is that my mindset (and probably my language as well) is wrong. Anyway, I have what I guess is a rather simple problem, that I now without success spent days trying to solve.
>>>
>>> I have a bunch of datasets imported from Stata that is labelled aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of one column only. The columns consists of integer numbers. I need to convert the data to vectors, which I found several ways to do. I use, for example:
>>> aa_2000 <- as.numeric(aa_2000[,1])
>>> However, when trying to automate the task, so I don't have to write a line of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is trying to make a loop in order to loop over all datasets. However, I manage to write a loop that works for the left-hand side of the syntax, but not for the right-hand side.
>>> I have included some examples from my struggles to solve the issue below, what they all have in common is that I don't manage to call for any "macro" (is that only a Stata-word?) in the right hand side of the functions. When I try to replace the static reference with a dynamic one (like in the left-hand side), the syntax just doesn't work.
>>>
>>> I would very much appreciate some help with this issue!
>>> All the best,
>>> Love
>>>
>>> year <- 2002
>>> dataname <- paste0("aa_",year)
>>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
>>>
>>> year <- 2003
>>> assign(paste0("aa_",year), as.numeric(aa_2003))
>>>
>>> year <- 2005
>>> assign(paste0("aa_",year), aa_2005[,1])
>>>
>>> list1 <- c(2000:2007)
>>> list1[c(7)]
>>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] 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: [hidden email]  Priv: [hidden email]
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>
> ______________________________________________
> [hidden email] 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 Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

R help mailing list-2
By the way, R 'vectors' are not the equivalents of mathematical 'vectors'.
In R, a vector is something that can have arbitrary length and which has
no 'attributes', other than perhaps element names.  Vectors can be numeric,
character,
complex, lists, etc.   Functions, names, and NULL are not vectors.  In my
opinion,
the typical data scientist will rarely find the R vector concept useful and
it more
likely to make mistakes with it.

Instead of using as.vector and is.vector, use as.numeric or as.character,
etc.,
and is.numeric or is.character, naming the type you want.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 4, 2017 at 4:23 PM, Love Bohman <[hidden email]>
wrote:

> Hi again!
> I know you don't find loops evil (well, at least not diabolic :-) ).
> (After many hours googling I have realized that thinking about loops rather
> than lists is a newbie thing we Stata-users do, I just jokingly pointed it
> out). Anyway, I'm really happy that you try to teach me some R-manners.
> Since I still get questions about what the h**k I mean by my strange
> question, I sort it out with an example:
>
> I had a number of matrices, named in a consecutive manner:
>
> aa_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0",
> header=FALSE))
> aa_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0",
> header=FALSE))
> aa_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0",
> header=FALSE))
>
> I needed them to be vectors, and they weren't:
>
> is.vector(aa_2000)
>
> I finally solved it with this loop (well, I admit I shaped up the last
> line thanks to William Dunlap):
>
> for (year in 2000:2002){
> varname <- paste0("aa_",year)
> assign(varname, as.vector(eval(as.name(varname))))
> }
>
> The loop obviously solved the problem:
>
> is.vector(aa_2000)
>
> However, you have taught me that I should have solved it more elegant with
> a data list:
>
> bb_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0",
> header=FALSE))
> bb_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0",
> header=FALSE))
> bb_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0",
> header=FALSE))
>
> is.vector(bb_2000)
>
> datanames <- paste0("bb_", 2000:2002)
> datalist <- lapply(datanames, get)
> is.vector(datalist[1])
>
>
> I learned a lot of code today, and I really appreciate it! A million
> thanks!
> My R-superpowers are, well, not as minuscule as when I woke up this
> morning.
>
> All the best,
> Love (or maybe LoveR, my future superhero name)
>
>
>
> -----Ursprungligt meddelande-----
> Från: Jeff Newmiller [mailto:[hidden email]]
> Skickat: den 5 december 2017 00:01
> Till: Love Bohman <[hidden email]>
> Kopia: peter dalgaard <[hidden email]>; [hidden email]
> Ämne: Re: [R] Dynamic reference, right-hand side of function
>
> Loops are not evil, and no-one in this thread said they are. But I believe
> your failure to provide a reproducible example is creating confusion, since
> you may be using words that mean one thing to you and something else to the
> readers here.
>
> ################################
> # A reproducible example includes a tiny set of sample data # Since we
> cannot reproducibly refer to filenames (your directories # and files in
> them are unlikely to be like mine), I will # use a little trick to read
> from data in the example:
>
> dta <- read.csv( text=
> "1.1,3.0,5,7.4,4,2.2,0
> ", header=FALSE)
> str(dta)
> #> 'data.frame':    1 obs. of  7 variables:
> #>  $ V1: num 1.1
> #>  $ V2: num 3
> #>  $ V3: int 5
> #>  $ V4: num 7.4
> #>  $ V5: int 4
> #>  $ V6: num 2.2
> #>  $ V7: int 0
>
> # note that I did not use "data" as the name because # there is a
> commonly-used function by that name in R # that could be confused with your
> variable
>
> # if you have your object already in memory, you can use the # dput
> function to create R code that will re-create it in our # working
> environments.
>
> dput( dta )
> #> structure(list(V1 = 1.1, V2 = 3, V3 = 5L, V4 = 7.4, V5 = 4L,
> #>     V6 = 2.2, V7 = 0L), .Names = c("V1", "V2", "V3", "V4", "V5",
> #> "V6", "V7"), class = "data.frame", row.names = c(NA, -1L))
>
> # which you can put a variable name in front of in your example code:
>
> dtasample <- structure(list( V1 = 1.1, V2 = 3, V3 = 5L , V4 = 7.4, V5 =
> 4L, V6 = 2.2, V7 = 0L ) , .Names = c( "V1"
> , "V2", "V3", "V4", "V5", "V6", "V7" ), class = "data.frame"
> , row.names = c(NA, -1L) )
>
> # and starting with that line you can make a self-contained #
> (reproducible) example for us to investigate your problem with
>
> # Note that reading a single row of data into R usually gets # a data
> frame, which looks like a matrix but is not a matrix.
> # Read the Introduction to R about these two types carefully.
> # Each column in a data frame can have a different type of data, # but in
> a vector or a matrix all rows and columns must be of # the same type.
>
> dtam <- as.matrix( dta )
>
> # If you have any values that R cannot clearly identify as numeric # or
> integer, then the next most general type of variable is # character... and
> that is often something that trips up newbies, # though I have no evidence
> that you have any non-numeric columns # in your data frames.
>
> dtax <- as.vector( dta )
> str(dtax)
> #> 'data.frame':    1 obs. of  7 variables:
> #>  $ V1: num 1.1
> #>  $ V2: num 3
> #>  $ V3: int 5
> #>  $ V4: num 7.4
> #>  $ V5: int 4
> #>  $ V6: num 2.2
> #>  $ V7: int 0
>
> # This actually makes no change to dta, because a data frame is already #
> a list of columns, and lists are just vectors that can hold different #
> types of variables, so dta is already a kind of vector.
>
> dtan <- as.numeric( dta )
> str(dtan)
> #>  num [1:7] 1.1 3 5 7.4 4 2.2 0
>
> # I suspect this is what you are trying to accomplish... but really, # if
> we had an example of the data you are working with, we would # already know.
> ################################
>
> Some more explanations of reproducibility [1][2][3]
>
> [1] http://stackoverflow.com/questions/5963269/how-to-make-
> a-great-r-reproducible-example
>
> [2] http://adv-r.had.co.nz/Reproducibility.html
>
> [3] https://cran.r-project.org/web/packages/reprex/index.html (read the
> vignette)
>
>
> On Mon, 4 Dec 2017, Love Bohman wrote:
>
> > :-)
> > I don't insist on anything, I'm just struggling to learn a new language
> and partly a new way of thinking, and I really appreciate the corrections.
> I hope I someday will be able to handle lists in R as easy as I handle
> loops in Stata...
> > Thanks again!
> >
> > Love
> >
> >
> > -----Ursprungligt meddelande-----
> > Fr?n: peter dalgaard [mailto:[hidden email]]
> > Skickat: den 4 december 2017 23:09
> > Till: Love Bohman <[hidden email]>
> > Kopia: [hidden email]
> > ?mne: Re: [R] Dynamic reference, right-hand side of function
> >
> > Um, if you insist on doing it that way, at least use
> >
> > assign(varname, as.vector(get(varname)))
> >
> > -pd
> >
> >> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]>
> wrote:
> >>
> >> Hi!
> >> Thanks for the replies!
> >> I understand people more accustomed to R doesn't like looping much, and
> that thinking about loops is something I do since I worked with Stata a
> lot. The syntax from Peter Dalgaard was really clever, and I learned a lot
> from it, even though it didn't solve my problem (I guess it wasn't very
> well explained). My problem was basically that I have a data matrix
> consisting of just 1 row, and I want to convert that row into a vector.
> However, when trying to do that dynamically, I couldn't get R to read the
> right hand side of the syntax as a variable name instead of a string.
> However, together with a colleague I finally solved it with the (eval(
> as.name()) function (I include the loop I used below). I understand that
> looping isn't kosher among you more devoted R-users, and eventually I hope
> I will learn to use lists in the future instead.
> >>
> >> Thanks!
> >> Love
> >>
> >>
> >> for (year in 2000:2007){
> >> varname <- paste0("aa_",year)
> >> assign(paste0(varname), as.vector(eval(as.name(varname))))
> >> }
> >>
> >> -----Ursprungligt meddelande-----
> >> Fr?n: peter dalgaard [mailto:[hidden email]]
> >> Skickat: den 4 december 2017 16:39
> >> Till: Love Bohman <[hidden email]>
> >> Kopia: [hidden email]
> >> ?mne: Re: [R] Dynamic reference, right-hand side of function
> >>
> >> The generic rule is that R is not a macro language, so looping of names
> of things gets awkward. It is usually easier to use compound objects like
> lists and iterate over them. E.g.
> >>
> >> datanames <- paste0("aa_", 2000:2007)
> >> datalist <- lapply(datanames, get)
> >> names(datalist) <- datanames
> >> col1 <- lapply(datalist, "[[", 1)
> >> colnum <- lapply(col1, as.numeric)
> >>
> >> (The 2nd line assumes that the damage has already been done so that
> >> you have aa_2000 ... aa_2007 in your workspace. You might
> >> alternatively create the list directly while importing the data.)
> >>
> >> -pd
> >>
> >>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]>
> wrote:
> >>>
> >>> Hi R-users!
> >>> Being new to R, and a fairly advanced Stata-user, I guess part of my
> problem is that my mindset (and probably my language as well) is wrong.
> Anyway, I have what I guess is a rather simple problem, that I now without
> success spent days trying to solve.
> >>>
> >>> I have a bunch of datasets imported from Stata that is labelled
> aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and
> consists of one column only. The columns consists of integer numbers. I
> need to convert the data to vectors, which I found several ways to do. I
> use, for example:
> >>> aa_2000 <- as.numeric(aa_2000[,1])
> >>> However, when trying to automate the task, so I don't have to write a
> line of code for each dataset, I get stuck. Since I'm a Stata user, my
> first attempt is trying to make a loop in order to loop over all datasets.
> However, I manage to write a loop that works for the left-hand side of the
> syntax, but not for the right-hand side.
> >>> I have included some examples from my struggles to solve the issue
> below, what they all have in common is that I don't manage to call for any
> "macro" (is that only a Stata-word?) in the right hand side of the
> functions. When I try to replace the static reference with a dynamic one
> (like in the left-hand side), the syntax just doesn't work.
> >>>
> >>> I would very much appreciate some help with this issue!
> >>> All the best,
> >>> Love
> >>>
> >>> year <- 2002
> >>> dataname <- paste0("aa_",year)
> >>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
> >>>
> >>> year <- 2003
> >>> assign(paste0("aa_",year), as.numeric(aa_2003))
> >>>
> >>> year <- 2005
> >>> assign(paste0("aa_",year), aa_2005[,1])
> >>>
> >>> list1 <- c(2000:2007)
> >>> list1[c(7)]
> >>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
> >>>
> >>>
> >>>     [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> [hidden email] 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: [hidden email]  Priv: [hidden email]
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> > --
> > Peter Dalgaard, Professor,
> > Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000
> Frederiksberg, Denmark
> > Phone: (+45)38153501
> > Office: A 4.23
> > Email: [hidden email]  Priv: [hidden email]
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > ______________________________________________
> > [hidden email] 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 Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live
> Go...
>                                        Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

Bert Gunter-2
It appears that much of the OP's confusion could be cleared up by studying
relevant resources, e.g. online tutorials or the R Language Maual that
ships with R. This list is not meant to replace such "homework" by users,
as this lengthy and confusing interchange demonstrates.

Having said that, the OP can check that a matrix of any dimensions is
represented in R as a vector in column major order with a "dim" attribute.
So:

> a <- matrix(1:10, nr = 5)
> dim(a)
[1] 5 2
> a ## invokes the print() method for matrices
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10
> is.vector(a)
[1] FALSE
> dim(a) <- NULL ## removes the 'dim' attribute
> is.vector(a)
[1] TRUE
> a
 [1]  1  2  3  4  5  6  7  8  9 10

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 Mon, Dec 4, 2017 at 4:40 PM, William Dunlap via R-help <
[hidden email]> wrote:

> By the way, R 'vectors' are not the equivalents of mathematical 'vectors'.
> In R, a vector is something that can have arbitrary length and which has
> no 'attributes', other than perhaps element names.  Vectors can be numeric,
> character,
> complex, lists, etc.   Functions, names, and NULL are not vectors.  In my
> opinion,
> the typical data scientist will rarely find the R vector concept useful and
> it more
> likely to make mistakes with it.
>
> Instead of using as.vector and is.vector, use as.numeric or as.character,
> etc.,
> and is.numeric or is.character, naming the type you want.
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Mon, Dec 4, 2017 at 4:23 PM, Love Bohman <[hidden email]>
> wrote:
>
> > Hi again!
> > I know you don't find loops evil (well, at least not diabolic :-) ).
> > (After many hours googling I have realized that thinking about loops
> rather
> > than lists is a newbie thing we Stata-users do, I just jokingly pointed
> it
> > out). Anyway, I'm really happy that you try to teach me some R-manners.
> > Since I still get questions about what the h**k I mean by my strange
> > question, I sort it out with an example:
> >
> > I had a number of matrices, named in a consecutive manner:
> >
> > aa_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0",
> > header=FALSE))
> > aa_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0",
> > header=FALSE))
> > aa_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0",
> > header=FALSE))
> >
> > I needed them to be vectors, and they weren't:
> >
> > is.vector(aa_2000)
> >
> > I finally solved it with this loop (well, I admit I shaped up the last
> > line thanks to William Dunlap):
> >
> > for (year in 2000:2002){
> > varname <- paste0("aa_",year)
> > assign(varname, as.vector(eval(as.name(varname))))
> > }
> >
> > The loop obviously solved the problem:
> >
> > is.vector(aa_2000)
> >
> > However, you have taught me that I should have solved it more elegant
> with
> > a data list:
> >
> > bb_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0",
> > header=FALSE))
> > bb_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0",
> > header=FALSE))
> > bb_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0",
> > header=FALSE))
> >
> > is.vector(bb_2000)
> >
> > datanames <- paste0("bb_", 2000:2002)
> > datalist <- lapply(datanames, get)
> > is.vector(datalist[1])
> >
> >
> > I learned a lot of code today, and I really appreciate it! A million
> > thanks!
> > My R-superpowers are, well, not as minuscule as when I woke up this
> > morning.
> >
> > All the best,
> > Love (or maybe LoveR, my future superhero name)
> >
> >
> >
> > -----Ursprungligt meddelande-----
> > Från: Jeff Newmiller [mailto:[hidden email]]
> > Skickat: den 5 december 2017 00:01
> > Till: Love Bohman <[hidden email]>
> > Kopia: peter dalgaard <[hidden email]>; [hidden email]
> > Ämne: Re: [R] Dynamic reference, right-hand side of function
> >
> > Loops are not evil, and no-one in this thread said they are. But I
> believe
> > your failure to provide a reproducible example is creating confusion,
> since
> > you may be using words that mean one thing to you and something else to
> the
> > readers here.
> >
> > ################################
> > # A reproducible example includes a tiny set of sample data # Since we
> > cannot reproducibly refer to filenames (your directories # and files in
> > them are unlikely to be like mine), I will # use a little trick to read
> > from data in the example:
> >
> > dta <- read.csv( text=
> > "1.1,3.0,5,7.4,4,2.2,0
> > ", header=FALSE)
> > str(dta)
> > #> 'data.frame':    1 obs. of  7 variables:
> > #>  $ V1: num 1.1
> > #>  $ V2: num 3
> > #>  $ V3: int 5
> > #>  $ V4: num 7.4
> > #>  $ V5: int 4
> > #>  $ V6: num 2.2
> > #>  $ V7: int 0
> >
> > # note that I did not use "data" as the name because # there is a
> > commonly-used function by that name in R # that could be confused with
> your
> > variable
> >
> > # if you have your object already in memory, you can use the # dput
> > function to create R code that will re-create it in our # working
> > environments.
> >
> > dput( dta )
> > #> structure(list(V1 = 1.1, V2 = 3, V3 = 5L, V4 = 7.4, V5 = 4L,
> > #>     V6 = 2.2, V7 = 0L), .Names = c("V1", "V2", "V3", "V4", "V5",
> > #> "V6", "V7"), class = "data.frame", row.names = c(NA, -1L))
> >
> > # which you can put a variable name in front of in your example code:
> >
> > dtasample <- structure(list( V1 = 1.1, V2 = 3, V3 = 5L , V4 = 7.4, V5 =
> > 4L, V6 = 2.2, V7 = 0L ) , .Names = c( "V1"
> > , "V2", "V3", "V4", "V5", "V6", "V7" ), class = "data.frame"
> > , row.names = c(NA, -1L) )
> >
> > # and starting with that line you can make a self-contained #
> > (reproducible) example for us to investigate your problem with
> >
> > # Note that reading a single row of data into R usually gets # a data
> > frame, which looks like a matrix but is not a matrix.
> > # Read the Introduction to R about these two types carefully.
> > # Each column in a data frame can have a different type of data, # but in
> > a vector or a matrix all rows and columns must be of # the same type.
> >
> > dtam <- as.matrix( dta )
> >
> > # If you have any values that R cannot clearly identify as numeric # or
> > integer, then the next most general type of variable is # character...
> and
> > that is often something that trips up newbies, # though I have no
> evidence
> > that you have any non-numeric columns # in your data frames.
> >
> > dtax <- as.vector( dta )
> > str(dtax)
> > #> 'data.frame':    1 obs. of  7 variables:
> > #>  $ V1: num 1.1
> > #>  $ V2: num 3
> > #>  $ V3: int 5
> > #>  $ V4: num 7.4
> > #>  $ V5: int 4
> > #>  $ V6: num 2.2
> > #>  $ V7: int 0
> >
> > # This actually makes no change to dta, because a data frame is already #
> > a list of columns, and lists are just vectors that can hold different #
> > types of variables, so dta is already a kind of vector.
> >
> > dtan <- as.numeric( dta )
> > str(dtan)
> > #>  num [1:7] 1.1 3 5 7.4 4 2.2 0
> >
> > # I suspect this is what you are trying to accomplish... but really, # if
> > we had an example of the data you are working with, we would # already
> know.
> > ################################
> >
> > Some more explanations of reproducibility [1][2][3]
> >
> > [1] http://stackoverflow.com/questions/5963269/how-to-make-
> > a-great-r-reproducible-example
> >
> > [2] http://adv-r.had.co.nz/Reproducibility.html
> >
> > [3] https://cran.r-project.org/web/packages/reprex/index.html (read the
> > vignette)
> >
> >
> > On Mon, 4 Dec 2017, Love Bohman wrote:
> >
> > > :-)
> > > I don't insist on anything, I'm just struggling to learn a new language
> > and partly a new way of thinking, and I really appreciate the
> corrections.
> > I hope I someday will be able to handle lists in R as easy as I handle
> > loops in Stata...
> > > Thanks again!
> > >
> > > Love
> > >
> > >
> > > -----Ursprungligt meddelande-----
> > > Fr?n: peter dalgaard [mailto:[hidden email]]
> > > Skickat: den 4 december 2017 23:09
> > > Till: Love Bohman <[hidden email]>
> > > Kopia: [hidden email]
> > > ?mne: Re: [R] Dynamic reference, right-hand side of function
> > >
> > > Um, if you insist on doing it that way, at least use
> > >
> > > assign(varname, as.vector(get(varname)))
> > >
> > > -pd
> > >
> > >> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]>
> > wrote:
> > >>
> > >> Hi!
> > >> Thanks for the replies!
> > >> I understand people more accustomed to R doesn't like looping much,
> and
> > that thinking about loops is something I do since I worked with Stata a
> > lot. The syntax from Peter Dalgaard was really clever, and I learned a
> lot
> > from it, even though it didn't solve my problem (I guess it wasn't very
> > well explained). My problem was basically that I have a data matrix
> > consisting of just 1 row, and I want to convert that row into a vector.
> > However, when trying to do that dynamically, I couldn't get R to read the
> > right hand side of the syntax as a variable name instead of a string.
> > However, together with a colleague I finally solved it with the (eval(
> > as.name()) function (I include the loop I used below). I understand that
> > looping isn't kosher among you more devoted R-users, and eventually I
> hope
> > I will learn to use lists in the future instead.
> > >>
> > >> Thanks!
> > >> Love
> > >>
> > >>
> > >> for (year in 2000:2007){
> > >> varname <- paste0("aa_",year)
> > >> assign(paste0(varname), as.vector(eval(as.name(varname))))
> > >> }
> > >>
> > >> -----Ursprungligt meddelande-----
> > >> Fr?n: peter dalgaard [mailto:[hidden email]]
> > >> Skickat: den 4 december 2017 16:39
> > >> Till: Love Bohman <[hidden email]>
> > >> Kopia: [hidden email]
> > >> ?mne: Re: [R] Dynamic reference, right-hand side of function
> > >>
> > >> The generic rule is that R is not a macro language, so looping of
> names
> > of things gets awkward. It is usually easier to use compound objects like
> > lists and iterate over them. E.g.
> > >>
> > >> datanames <- paste0("aa_", 2000:2007)
> > >> datalist <- lapply(datanames, get)
> > >> names(datalist) <- datanames
> > >> col1 <- lapply(datalist, "[[", 1)
> > >> colnum <- lapply(col1, as.numeric)
> > >>
> > >> (The 2nd line assumes that the damage has already been done so that
> > >> you have aa_2000 ... aa_2007 in your workspace. You might
> > >> alternatively create the list directly while importing the data.)
> > >>
> > >> -pd
> > >>
> > >>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]>
> > wrote:
> > >>>
> > >>> Hi R-users!
> > >>> Being new to R, and a fairly advanced Stata-user, I guess part of my
> > problem is that my mindset (and probably my language as well) is wrong.
> > Anyway, I have what I guess is a rather simple problem, that I now
> without
> > success spent days trying to solve.
> > >>>
> > >>> I have a bunch of datasets imported from Stata that is labelled
> > aa_2000 aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and
> > consists of one column only. The columns consists of integer numbers. I
> > need to convert the data to vectors, which I found several ways to do. I
> > use, for example:
> > >>> aa_2000 <- as.numeric(aa_2000[,1])
> > >>> However, when trying to automate the task, so I don't have to write a
> > line of code for each dataset, I get stuck. Since I'm a Stata user, my
> > first attempt is trying to make a loop in order to loop over all
> datasets.
> > However, I manage to write a loop that works for the left-hand side of
> the
> > syntax, but not for the right-hand side.
> > >>> I have included some examples from my struggles to solve the issue
> > below, what they all have in common is that I don't manage to call for
> any
> > "macro" (is that only a Stata-word?) in the right hand side of the
> > functions. When I try to replace the static reference with a dynamic one
> > (like in the left-hand side), the syntax just doesn't work.
> > >>>
> > >>> I would very much appreciate some help with this issue!
> > >>> All the best,
> > >>> Love
> > >>>
> > >>> year <- 2002
> > >>> dataname <- paste0("aa_",year)
> > >>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
> > >>>
> > >>> year <- 2003
> > >>> assign(paste0("aa_",year), as.numeric(aa_2003))
> > >>>
> > >>> year <- 2005
> > >>> assign(paste0("aa_",year), aa_2005[,1])
> > >>>
> > >>> list1 <- c(2000:2007)
> > >>> list1[c(7)]
> > >>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
> > >>>
> > >>>
> > >>>     [[alternative HTML version deleted]]
> > >>>
> > >>> ______________________________________________
> > >>> [hidden email] 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: [hidden email]  Priv: [hidden email]
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >
> > > --
> > > Peter Dalgaard, Professor,
> > > Center for Statistics, Copenhagen Business School Solbjerg Plads 3,
> 2000
> > Frederiksberg, Denmark
> > > Phone: (+45)38153501
> > > Office: A 4.23
> > > Email: [hidden email]  Priv: [hidden email]
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > ______________________________________________
> > > [hidden email] 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 Newmiller                        The     .....       .....  Go
> Live...
> > DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live
> > Go...
> >                                        Live:   OO#.. Dead: OO#..  Playing
> > Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> > /Software/Embedded Controllers)               .OO#.       .OO#.
> rocks...1k
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic reference, right-hand side of function

PIKAL Petr
In reply to this post by Love Bohman
Hi

I am just curious why not to set proper form of object directly when you read it to R?

Based on your example

aa_2000 <- as.vector(as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0", header=FALSE)))

or perhaps

> aa_2000<-unlist(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0", header=FALSE))
> is.vector(aa_2000)
[1] TRUE

But as others pointed out you should understand what are R objects and how you could use them.

When you want to read several files to R, my concept would be to put them in separate directory, change the working directory in R to this directory with my files,

?setwd

make vector of file names
myfiles <- list.files()

?list.files

and iterate through this vector of files

for (i in myfiles) {

do read stuff
do transform stuff
do concatenation stuff
}

to end with one object (list, data.frame, ...) which is suitable for further analysis and/or manipulation.

Cheers
Petr

> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Love Bohman
> Sent: Tuesday, December 5, 2017 1:23 AM
> To: Jeff Newmiller <[hidden email]>
> Cc: [hidden email]; peter dalgaard <[hidden email]>
> Subject: Re: [R] Dynamic reference, right-hand side of function
>
> Hi again!
> I know you don't find loops evil (well, at least not diabolic :-) ). (After many
> hours googling I have realized that thinking about loops rather than lists is a
> newbie thing we Stata-users do, I just jokingly pointed it out). Anyway, I'm
> really happy that you try to teach me some R-manners. Since I still get
> questions about what the h**k I mean by my strange question, I sort it out with
> an example:
>
> I had a number of matrices, named in a consecutive manner:
>
> aa_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0", header=FALSE))
> aa_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0",
> header=FALSE))
> aa_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0",
> header=FALSE))
>
> I needed them to be vectors, and they weren't:
>
> is.vector(aa_2000)
>
> I finally solved it with this loop (well, I admit I shaped up the last line thanks to
> William Dunlap):
>
> for (year in 2000:2002){
> varname <- paste0("aa_",year)
> assign(varname, as.vector(eval(as.name(varname))))
> }
>
> The loop obviously solved the problem:
>
> is.vector(aa_2000)
>
> However, you have taught me that I should have solved it more elegant with a
> data list:
>
> bb_2000 <- as.matrix(read.csv(text="1,0,1,1,0,0,0,0,0,0,1,0,0", header=FALSE))
> bb_2001 <- as.matrix(read.csv( text="0,0,0,1,0,1,1,0,0,0,0,1,0,0",
> header=FALSE))
> bb_2002 <- as.matrix(read.csv( text="1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0",
> header=FALSE))
>
> is.vector(bb_2000)
>
> datanames <- paste0("bb_", 2000:2002)
> datalist <- lapply(datanames, get)
> is.vector(datalist[1])
>
>
> I learned a lot of code today, and I really appreciate it! A million thanks!
> My R-superpowers are, well, not as minuscule as when I woke up this morning.
>
> All the best,
> Love (or maybe LoveR, my future superhero name)
>
>
>
> -----Ursprungligt meddelande-----
> Från: Jeff Newmiller [mailto:[hidden email]]
> Skickat: den 5 december 2017 00:01
> Till: Love Bohman <[hidden email]>
> Kopia: peter dalgaard <[hidden email]>; [hidden email]
> Ämne: Re: [R] Dynamic reference, right-hand side of function
>
> Loops are not evil, and no-one in this thread said they are. But I believe your
> failure to provide a reproducible example is creating confusion, since you may
> be using words that mean one thing to you and something else to the readers
> here.
>
> ################################
> # A reproducible example includes a tiny set of sample data # Since we cannot
> reproducibly refer to filenames (your directories # and files in them are unlikely
> to be like mine), I will # use a little trick to read from data in the example:
>
> dta <- read.csv( text=
> "1.1,3.0,5,7.4,4,2.2,0
> ", header=FALSE)
> str(dta)
> #> 'data.frame':    1 obs. of  7 variables:
> #>  $ V1: num 1.1
> #>  $ V2: num 3
> #>  $ V3: int 5
> #>  $ V4: num 7.4
> #>  $ V5: int 4
> #>  $ V6: num 2.2
> #>  $ V7: int 0
>
> # note that I did not use "data" as the name because # there is a commonly-
> used function by that name in R # that could be confused with your variable
>
> # if you have your object already in memory, you can use the # dput function to
> create R code that will re-create it in our # working environments.
>
> dput( dta )
> #> structure(list(V1 = 1.1, V2 = 3, V3 = 5L, V4 = 7.4, V5 = 4L,
> #>     V6 = 2.2, V7 = 0L), .Names = c("V1", "V2", "V3", "V4", "V5",
> #> "V6", "V7"), class = "data.frame", row.names = c(NA, -1L))
>
> # which you can put a variable name in front of in your example code:
>
> dtasample <- structure(list( V1 = 1.1, V2 = 3, V3 = 5L , V4 = 7.4, V5 = 4L, V6 =
> 2.2, V7 = 0L ) , .Names = c( "V1"
> , "V2", "V3", "V4", "V5", "V6", "V7" ), class = "data.frame"
> , row.names = c(NA, -1L) )
>
> # and starting with that line you can make a self-contained # (reproducible)
> example for us to investigate your problem with
>
> # Note that reading a single row of data into R usually gets # a data frame,
> which looks like a matrix but is not a matrix.
> # Read the Introduction to R about these two types carefully.
> # Each column in a data frame can have a different type of data, # but in a
> vector or a matrix all rows and columns must be of # the same type.
>
> dtam <- as.matrix( dta )
>
> # If you have any values that R cannot clearly identify as numeric # or integer,
> then the next most general type of variable is # character... and that is often
> something that trips up newbies, # though I have no evidence that you have
> any non-numeric columns # in your data frames.
>
> dtax <- as.vector( dta )
> str(dtax)
> #> 'data.frame':    1 obs. of  7 variables:
> #>  $ V1: num 1.1
> #>  $ V2: num 3
> #>  $ V3: int 5
> #>  $ V4: num 7.4
> #>  $ V5: int 4
> #>  $ V6: num 2.2
> #>  $ V7: int 0
>
> # This actually makes no change to dta, because a data frame is already # a list
> of columns, and lists are just vectors that can hold different # types of
> variables, so dta is already a kind of vector.
>
> dtan <- as.numeric( dta )
> str(dtan)
> #>  num [1:7] 1.1 3 5 7.4 4 2.2 0
>
> # I suspect this is what you are trying to accomplish... but really, # if we had an
> example of the data you are working with, we would # already know.
> ################################
>
> Some more explanations of reproducibility [1][2][3]
>
> [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-
> reproducible-example
>
> [2] http://adv-r.had.co.nz/Reproducibility.html
>
> [3] https://cran.r-project.org/web/packages/reprex/index.html (read the
> vignette)
>
>
> On Mon, 4 Dec 2017, Love Bohman wrote:
>
> > :-)
> > I don't insist on anything, I'm just struggling to learn a new language and
> partly a new way of thinking, and I really appreciate the corrections. I hope I
> someday will be able to handle lists in R as easy as I handle loops in Stata...
> > Thanks again!
> >
> > Love
> >
> >
> > -----Ursprungligt meddelande-----
> > Fr?n: peter dalgaard [mailto:[hidden email]]
> > Skickat: den 4 december 2017 23:09
> > Till: Love Bohman <[hidden email]>
> > Kopia: [hidden email]
> > ?mne: Re: [R] Dynamic reference, right-hand side of function
> >
> > Um, if you insist on doing it that way, at least use
> >
> > assign(varname, as.vector(get(varname)))
> >
> > -pd
> >
> >> On 4 Dec 2017, at 22:46 , Love Bohman <[hidden email]>
> wrote:
> >>
> >> Hi!
> >> Thanks for the replies!
> >> I understand people more accustomed to R doesn't like looping much, and
> that thinking about loops is something I do since I worked with Stata a lot. The
> syntax from Peter Dalgaard was really clever, and I learned a lot from it, even
> though it didn't solve my problem (I guess it wasn't very well explained). My
> problem was basically that I have a data matrix consisting of just 1 row, and I
> want to convert that row into a vector. However, when trying to do that
> dynamically, I couldn't get R to read the right hand side of the syntax as a
> variable name instead of a string. However, together with a colleague I finally
> solved it with the (eval(as.name()) function (I include the loop I used below). I
> understand that looping isn't kosher among you more devoted R-users, and
> eventually I hope I will learn to use lists in the future instead.
> >>
> >> Thanks!
> >> Love
> >>
> >>
> >> for (year in 2000:2007){
> >> varname <- paste0("aa_",year)
> >> assign(paste0(varname), as.vector(eval(as.name(varname))))
> >> }
> >>
> >> -----Ursprungligt meddelande-----
> >> Fr?n: peter dalgaard [mailto:[hidden email]]
> >> Skickat: den 4 december 2017 16:39
> >> Till: Love Bohman <[hidden email]>
> >> Kopia: [hidden email]
> >> ?mne: Re: [R] Dynamic reference, right-hand side of function
> >>
> >> The generic rule is that R is not a macro language, so looping of names of
> things gets awkward. It is usually easier to use compound objects like lists and
> iterate over them. E.g.
> >>
> >> datanames <- paste0("aa_", 2000:2007) datalist <- lapply(datanames,
> >> get)
> >> names(datalist) <- datanames
> >> col1 <- lapply(datalist, "[[", 1)
> >> colnum <- lapply(col1, as.numeric)
> >>
> >> (The 2nd line assumes that the damage has already been done so that
> >> you have aa_2000 ... aa_2007 in your workspace. You might
> >> alternatively create the list directly while importing the data.)
> >>
> >> -pd
> >>
> >>> On 4 Dec 2017, at 12:33 , Love Bohman <[hidden email]>
> wrote:
> >>>
> >>> Hi R-users!
> >>> Being new to R, and a fairly advanced Stata-user, I guess part of my
> problem is that my mindset (and probably my language as well) is wrong.
> Anyway, I have what I guess is a rather simple problem, that I now without
> success spent days trying to solve.
> >>>
> >>> I have a bunch of datasets imported from Stata that is labelled aa_2000
> aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of
> one column only. The columns consists of integer numbers. I need to convert
> the data to vectors, which I found several ways to do. I use, for example:
> >>> aa_2000 <- as.numeric(aa_2000[,1])
> >>> However, when trying to automate the task, so I don't have to write a line
> of code for each dataset, I get stuck. Since I'm a Stata user, my first attempt is
> trying to make a loop in order to loop over all datasets. However, I manage to
> write a loop that works for the left-hand side of the syntax, but not for the
> right-hand side.
> >>> I have included some examples from my struggles to solve the issue below,
> what they all have in common is that I don't manage to call for any "macro" (is
> that only a Stata-word?) in the right hand side of the functions. When I try to
> replace the static reference with a dynamic one (like in the left-hand side), the
> syntax just doesn't work.
> >>>
> >>> I would very much appreciate some help with this issue!
> >>> All the best,
> >>> Love
> >>>
> >>> year <- 2002
> >>> dataname <- paste0("aa_",year)
> >>> assign(paste0(dataname), as.numeric(aa_2002[,1]))
> >>>
> >>> year <- 2003
> >>> assign(paste0("aa_",year), as.numeric(aa_2003))
> >>>
> >>> year <- 2005
> >>> assign(paste0("aa_",year), aa_2005[,1])
> >>>
> >>> list1 <- c(2000:2007)
> >>> list1[c(7)]
> >>> assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
> >>>
> >>>
> >>>   [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> [hidden email] 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: [hidden email]  Priv: [hidden email]
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> > --
> > Peter Dalgaard, Professor,
> > Center for Statistics, Copenhagen Business School Solbjerg Plads 3,
> > 2000 Frederiksberg, Denmark
> > Phone: (+45)38153501
> > Office: A 4.23
> > Email: [hidden email]  Priv: [hidden email]
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > ______________________________________________
> > [hidden email] 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 Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
>                                        Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>
> ______________________________________________
> [hidden email] 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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
______________________________________________
[hidden email] 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.