[R] Dynamic reference, right-hand side of function

PIKAL Petr
Thu, 07 Dec 2017 08:57:59 -0000

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:r-help-bounces at r-project.org] On Behalf Of Love
Bohman
> Sent: Tuesday, December 5, 2017 1:23 AM
> To: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
> Cc: r-help at r-project.org; peter dalgaard <pdalgd at gmail.com>
> 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:jdnewmil at dcn.davis.ca.us]
> Skickat: den 5 december 2017 00:01
> Till: Love Bohman <love.bohman at sociology.su.se>
> Kopia: peter dalgaard <pdalgd at gmail.com>; r-help at r-project.org
> ?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:pdalgd at gmail.com]
> > Skickat: den 4 december 2017 23:09
> > Till: Love Bohman <love.bohman at sociology.su.se>
> > Kopia: r-help at r-project.org
> > ?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 <love.bohman at
sociology.su.se>
> 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:pdalgd at gmail.com]
> >> Skickat: den 4 december 2017 16:39
> >> Till: Love Bohman <love.bohman at sociology.su.se>
> >> Kopia: r-help at r-project.org
> >> ?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 <love.bohman at
sociology.su.se>
> 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]]
> >>>
> >>> ______________________________________________
> >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and
more, see
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide
> >>> http://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible
code.
> >>
> >> --
> >> Peter Dalgaard, Professor,
> >> Center for Statistics, Copenhagen Business School Solbjerg Plads
3,
> >> 2000 Frederiksberg, Denmark
> >> Phone: (+45)38153501
> >> Office: A 4.23
> >> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> > --
> > Peter Dalgaard, Professor,
> > Center for Statistics, Copenhagen Business School Solbjerg Plads 3,
> > 2000 Frederiksberg, Denmark
> > Phone: (+45)38153501
> > Office: A 4.23
> > Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.
Live Go...
>                                        Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

________________________________
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.