I fear I'm missing something obvious but . . . I have a list in R with structure
List of 752
$ : Named chr "金銀"
..- attr(*, "names")= chr "名詞"
$ : Named chr "吹替"
..- attr(*, "names")= chr "名詞"
$ : Named chr "献言"
It's easy enough to get the top level of the list with unlist(my_object). But how can I capture the attributes? I can use unlist(attributes(my_object[[n]])) for each element n. But surely there's a quick way to get this into a data frame. What am I missing?
The [[ operator can be used to extract single elements from a list. Here we extract the first element of the list. The [[ operator can also use named indices so that you don't have to remember the exact ordering of every element of the list. You can also use the $ operator to extract elements by name.
Getting attributes of Objects in R Language – attributes() and attr() Function. attribute() function in R Programming Language is used to get all the attributes of data. This function is also used to set new attributes to data.
To extract (also known as indexing or subscripting) one or more values (more generally known as elements) from a vector we use the square bracket [ ] notation.
To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).
lapply(yourlist, attributes)
will apply the attributes
function to each element in yourlist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With