Append Histogram Class Objects to a list elements in R

My title's problem is very similar to a lot of's in SOF, though my problem is very different : I got a list MY_LIST.

MY_LIST=list("a","b","c")

I got a histogram class object HIST_OBJECT.

HIST_OBJECT=hist(rnorm(n=500, m=1, sd=1))
class(HIST_OBJECT)
[1] "histogram"

I want to load my HIST_OBJECT into the 1st slot of my list MY_LIST. So i execute that :

MY_LIST[[1]][2]=list(HIST_OBJECT)

So far so good but the problem is that the class of my HIST_OBJECT has changed. It was an histogram class that i could plot (plot(HIST_OBJECT) works!) but now it's a list class that I can't plot anymore (plot(MY_LIST[[1]][2]) don't work)

class(MY_LIST[[1]][2])
[1] "list"

Does anyone know how to do in order to make MY_LIST[[1]][2] object an "histogram" class object? Thanks a lot in advance!!

Answers


You were close:

class(MY_LIST[[1]][[2]])

Think of a list like a train: a single [ gets the carriage you want with all its contents, a double [[ gets just the contents of that carriage. Also think why MY_LIST[[1]][2]=HIST_OBJECT does not work, but MY_LIST[[1]][[2]]=HIST_OBJECT does.

In this case, the [2] got you the same class as its container - a list, containing the second element, your hist. The [[2]] will get you the element in the second container, in this case your hist.

Another example:

df <- data.frame(x=c(1,2,3), y=c(4,5,6))
class(df[1])
class(df[[1]])

Need Your Help

Datas for report from mysql table

mysql mysqli

I am Creating a report for my website where I would be listing property prices at various

Embed generics inside struct

generics rust

I find difficulties when using Rust traits, so for example which is the correct way to do this?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.