How to edit multi-person objects in R -
i find following behaviour of r person object rather unexpected:
let's create multi-person object:
a = c(person("huck", "finn"), person("tom", "sawyer")) imagine want update given name of 1 person in object:
a[[1]]$given <- 'huckleberry' then if inspect our object, surprise have:
> [1] " <> [] ()" "tom sawyer" where'd huckleberry finn go?! (note if try single person object, works fine.) why happen?
how can above more logical behavior of correcting first name?
the syntax want here
a <- c(person("huck", "finn"), person("tom", "sawyer")) a[1]$given<-"huckleberry" #[1] "huckleberry finn" "tom sawyer" a group of people still "person" , has it's own special indexing function [.person , concat function c.person has perhaps different behavior expecting. problem [[ ]] messing underlying hidden list.
actually, it's interesting because they've overloaded indexing methods person not [<- or [[<- , that's what's causing error. because here, we're same
`$<-`(`[`(a,1), "given", "huckleberry") #works `$<-`(`[[`(a,1), "given", "huckleberry") #works but when
`[<-`(a, 1, `$<-`(`[`(a,1), "given", "huckleberry")) #works `[[<-`(a, 1, `$<-`(`[[`(a,1), "given", "huckleberry")) #no work we see difference. special wrapping/unwrapping happens during retrieval not happen during assignment.
so what's going on "person" list of lists. outer list holds people , inner lists hold data. can think of data this
x<-list( list(name="a"),list(name="b") ) y<-list( list(name="c") ) where x collection of 2 people , y "single" person. when do
x[1]<-y x you end with
list( list(name="c"),list(name="b") ) since you're replacing list list how [ indexing works lists. if try replace element @ [[1]] list of lists, list nested. example
x[[1]]<-y x becomes
x<-list( list(list(name="c")),list(name="b") ) and level of nesting what's confusing r when goes print person in first position. first person won't have named elements @ second level, when goes print, return
emptyp <- structure(list(structure(list(), class="person")), class="person") utils:::format.person(emptyp) # " <> [] ()" which gives symbols it's trying place name, e-mail address, role, , comment.
Comments
Post a Comment