0

My goal is to administer both variables and their values in a spreadsheet. Basically I want to be able to add the new values for a new year in a new column and load them into R. I then want to assign the variables named in the first column with the corresponding value in either one of the second or third column.

Input spreadsheet:

Variable Year2013 Year2018
age 12 17
pets c(cat,dog,elephant) c(dog,mouse)
cars cars$name cars$name

Desired Output: For year 2013

import("dataspreadsheet.csv")

derived from this -->

age <- 12
pets <- c(cat,dog,elephant)
cars <- cars$name

Is there any way to tell R to make this assignment?

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • 2
    I know a possibility to do this but I think that's a terrible idea. What are you trying to achieve? – Martin Gal Apr 11 '22 at 22:41
  • 1
    Would you consider transposing the data so that the rows become columns? Also you can look at the open.xlsx package for doing a lot of Excel handling. I'm a bit confused about the content of the cars row. – Elin Apr 12 '22 at 01:55
  • My goal is to create an import routine for a multi-wave research project. The values should be administered in Excel for a better overview over the changes between the years. The variables should be included for the same reason and for staff to easily expand the variables set. Ultimately, I would like to have variables in one column and values/code in the others. Do you think this is possible or do you know a cleaner way? @MartinGal – mamacanoa Apr 13 '22 at 13:46
  • I have considered using the wide format but haven't found the right approach. I will check out the package, thank you! @Elin – mamacanoa Apr 13 '22 at 13:51
  • That `cars$name` is a little bit puzzling. Do you refer always to the same data.frame (`cars` in this case) or might it be `cars$name` in one year and `mtcars$hp` in another? – Martin Gal Apr 13 '22 at 15:34
  • @MartinGal the data.frame would be the same every year. For this input it would also be possible to hard code it, as it does not change over from year to year. Then again I would not have all variables in one place. – mamacanoa Apr 15 '22 at 13:22
  • So those things are strings and you only will ever use them as strings, not try to put them into code? I think when you have year usually what you want is a long format with year as a column. – Elin Apr 15 '22 at 16:01
  • See https://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string and particularly https://stackoverflow.com/a/40164111/18753408. Martin is probably right about this being the wrong way to accomplish your data governance goals. If you really want to govern data in Excel, I'd recommend for starters that you specify variables and values in Excel without using R code. – Emmanuel Apr 16 '22 at 19:21

0 Answers0