But for data.frame the colnames can be duplicated. Am I right? Regards. On Tue, Nov 2, 2021 at 6:29 PM Jan van der Laan <rhelp at eoos.dds.nl> wrote:> > True, but in a lot of cases where a python user might use a dict an R > user will probably use a list; or when we are talking about arrays of > dicts in python, the R solution will probably be a data.frame (with each > dict field in a separate column). > > Jan > > > > > On 02-11-2021 11:18, Eric Berger wrote: > > One choice is > > new.env(hash=TRUE) > > in the base package > > > > > > > > On Tue, Nov 2, 2021 at 11:48 AM Yonghua Peng <yong at pobox.com> wrote: > > > >> I know this is a newbie question. But how do I implement the hash > structure > >> which is available in other languages (in python it's dict)? > >> > >> I know there is the list, but list's names can be duplicated here. > >> > >>> x <- list(x=1:5,y=month.name,x=3:7) > >> > >>> x > >> > >> $x > >> > >> [1] 1 2 3 4 5 > >> > >> > >> $y > >> > >> [1] "January" "February" "March" "April" "May" "June" > >> > >> [7] "July" "August" "September" "October" "November" > "December" > >> > >> > >> $x > >> > >> [1] 3 4 5 6 7 > >> > >> > >> > >> Thanks a lot. > >> > >> [[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. > >> > > > > [[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. > > > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Yes. A data.frame is basically a list where all elements are vectors of
the same length. So this issue also exists in a data.frame. However, the
data.frame construction method will detect this and generate unique
names (which also might not be what you want):
> data.frame(a=1:3, a=1:3)
a a.1
1 1
1
2 2 2
3 3 3
But still with a little effort you can still create a data.frame with
multiple columns with the same name. But as Duncan Murdoch mentions you
can usually control for that.
Best,
Jan
On 02-11-2021 11:32, Yonghua Peng wrote:> But for data.frame the colnames can be duplicated. Am I right?
>
> Regards.
>
> On Tue, Nov 2, 2021 at 6:29 PM Jan van der Laan <rhelp at eoos.dds.nl
> <mailto:rhelp at eoos.dds.nl>> wrote:
>
>
> True, but in a lot of cases where a python user might use a dict an R
> user will probably use a list; or when we are talking about arrays of
> dicts in python, the R solution will probably be a data.frame (with
> each
> dict field in a separate column).
>
> Jan
>
>
>
>
> On 02-11-2021 11:18, Eric Berger wrote:
> > One choice is
> > new.env(hash=TRUE)
> > in the base package
> >
> >
> >
> > On Tue, Nov 2, 2021 at 11:48 AM Yonghua Peng <yong at
pobox.com
> <mailto:yong at pobox.com>> wrote:
> >
> >> I know this is a newbie question. But how do I implement the
> hash structure
> >> which is available in other languages (in python it's
dict)?
> >>
> >> I know there is the list, but list's names can be
duplicated here.
> >>
> >>> x <- list(x=1:5,y=month.name
<http://month.name>,x=3:7)
> >>
> >>> x
> >>
> >> $x
> >>
> >> [1] 1 2 3 4 5
> >>
> >>
> >> $y
> >>
> >>? ?[1] "January"? ?"February"?
"March"? ? ?"April"? ? ?"May"
> ?"June"
> >>
> >>? ?[7] "July"? ? ? "August"? ?
"September" "October"
> ?"November"? "December"
> >>
> >>
> >> $x
> >>
> >> [1] 3 4 5 6 7
> >>
> >>
> >>
> >> Thanks a lot.
> >>
> >>? ? ? ? ? [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help at r-project.org <mailto:R-help at
r-project.org> mailing list
> -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible
code.
> >>
> >
> >? ? ? ?[[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org <mailto:R-help at r-project.org>
mailing list
> -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible
code.
> >
>
> ______________________________________________
> R-help at r-project.org <mailto:R-help at r-project.org> mailing
list --
> To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
If you're thinking about using environments, I would suggest you initialize them like x <- new.env(parent = emptyenv()) Since environments have parent environments, it means that requesting a value from that environment can actually return the value stored in a parent environment (this isn't an issue for [[ or $, this is exclusively an issue with assign, get, and exists) Or, if you've already got your values stored in a list that you want to turn into an environment: x <- list2env(listOfValues, parent = emptyenv()) Hope this helps! On Tue, Nov 2, 2021, 06:49 Yonghua Peng <yong at pobox.com> wrote:> But for data.frame the colnames can be duplicated. Am I right? > > Regards. > > On Tue, Nov 2, 2021 at 6:29 PM Jan van der Laan <rhelp at eoos.dds.nl> wrote: > > > > > True, but in a lot of cases where a python user might use a dict an R > > user will probably use a list; or when we are talking about arrays of > > dicts in python, the R solution will probably be a data.frame (with each > > dict field in a separate column). > > > > Jan > > > > > > > > > > On 02-11-2021 11:18, Eric Berger wrote: > > > One choice is > > > new.env(hash=TRUE) > > > in the base package > > > > > > > > > > > > On Tue, Nov 2, 2021 at 11:48 AM Yonghua Peng <yong at pobox.com> wrote: > > > > > >> I know this is a newbie question. But how do I implement the hash > > structure > > >> which is available in other languages (in python it's dict)? > > >> > > >> I know there is the list, but list's names can be duplicated here. > > >> > > >>> x <- list(x=1:5,y=month.name,x=3:7) > > >> > > >>> x > > >> > > >> $x > > >> > > >> [1] 1 2 3 4 5 > > >> > > >> > > >> $y > > >> > > >> [1] "January" "February" "March" "April" "May" > "June" > > >> > > >> [7] "July" "August" "September" "October" "November" > > "December" > > >> > > >> > > >> $x > > >> > > >> [1] 3 4 5 6 7 > > >> > > >> > > >> > > >> Thanks a lot. > > >> > > >> [[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. > > >> > > > > > > [[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. > > > > > > > ______________________________________________ > > 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. > > > > [[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. >[[alternative HTML version deleted]]