Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract attributes from R list

Tags:

list

dataframe

r

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?

like image 514
Mark R Avatar asked Feb 16 '17 18:02

Mark R


People also ask

How do I extract elements from a list in R?

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.

How do I get attributes in R?

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.

How do I extract elements from a vector in R?

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.

How do I extract the first element of a list in R?

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


1 Answers

lapply(yourlist, attributes)

will apply the attributes function to each element in yourlist

like image 182
Dason Avatar answered Oct 12 '22 22:10

Dason