tushaR
tushaR

Reputation: 3116

Adding a data.frame to an existing list containing vectors

I have a list which contains some vectors. For example "myList" looks like this:

myList

$mean
[1] 2    3    4    5    6    7    8    9    0    10

$sd
[1] 3    5    3

I want to add a new data.frame "data" to this existing list such that "myList" has three elements: myList$mean, myList$sd and myList$data.

How can this be done?

Upvotes: 1

Views: 1899

Answers (2)

zx8754
zx8754

Reputation: 56004

Try this example:

#existing list
myList <- list(mean=c(1:10),
               sd=c(11:15))

#new dataframe
df1 <- data.frame(x=1:10,
                  y=11:20)
#add dataframe
myList$df <- df1

#result
str(myList)

#output
# List of 3
# $ mean: int [1:10] 1 2 3 4 5 6 7 8 9 10
# $ sd  : int [1:5] 11 12 13 14 15
# $ df  :'data.frame':  10 obs. of  2 variables:
#   ..$ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
# ..$ y: int [1:10] 11 12 13 14 15 16 17 18 19 20

Upvotes: 2

dimitris_ps
dimitris_ps

Reputation: 5951

Try

myList$data <- df

where df is your data frame

Upvotes: 3

Related Questions