1

How can I store a dataframe inside a vector c() by name, without replacing fields inside the vector?

I am trying to store a dataframe by name in a vector in the same way that I am able to store Hello World with the key a_value:

> data <- c()
> data['a_value'] <- 'Hello World'
> data['a_value']
      a_value
"Hello World"

Now with the data frame:

> data['loaded_csv'] <- read.csv('test.csv', header=TRUE, sep=',')
Warning message:
In data["loaded_csv"] <- read.csv("test.csv", header = TRUE, sep = ",") :
  number of items to replace is not a multiple of replacement length

I know that R is trying to replace/update fields of data with the columns loaded from the read.csv call, but I don't want to do that. I only want to associate the data frame from read.csv(...) with the key loaded_csv inside of data. I'm sure this is due to a misunderstanding on my part of how R uses vectors.

I'd like to be able to do this after storing it:

loaded_csv <- data['loaded_csv']

Something similar in Python:

data = {}
data['a_value'] = 'Hello World' # works!
data['loaded_csv'] = read_csv('test.csv')

loaded_csv = data['loaded_csv'] # works!

UPDATE: trying with list() instead of c()

Lists don't seem to make a difference :^/

> data <- list()
> data['a_value'] <- 'Hello World'
> data['a_value']
$a_value
[1] "Hello World"

> data['loaded_csv'] <- read.csv('test.csv', header=TRUE, sep=',')
Warning message:
In data["loaded_csv"] <- read.csv("test.csv", header = TRUE, sep = ",") :
  number of items to replace is not a multiple of replacement length
d0c_s4vage
  • 3,947
  • 6
  • 23
  • 32

0 Answers0