0

Say I had a dataframe:

get(data) #get() needs to be used here

And a list of its column names - or to be exact only one specific (there will always be only one):

> columnName
[1] "Type"

Next I need to refer to this column to change data type (and later I'll have to refer to the column many times, for plots and etc.):

> get(data)$columnName <- as.factor(get(data)$columnName)
Error in `$<-.data.frame`(`*tmp*`, columnName, value = integer(0)) :
replacement has 0 rows, data has 448

It means that I need to perform this in the end:

data$Type <- as.factor(data$Type)

I searched for this error but couldn't find anything that could resolve this. I'd be grateful if someone could help me resolve this.

  • 1
    `$` only works with unquoted column names, use `[[columnName]]` when a name is stored in a string, so `get(data)[[columnName]]` in your example. – Gregor Thomas May 26 '21 at 15:32
  • @GregorThomas, thank you for the answer. It is almost what I need because when I try to use `get(data)[[columnName]] – alextheexplorerr May 26 '21 at 15:42
  • 1
    Yeah, you can't assign back to objects accessed with `get`. Sorry, I focused on the first issue of your question. For your problem as stated, you can make a local copy of the object, `this_data – Gregor Thomas May 26 '21 at 16:58
  • @GregorThomas, we're getting closer to the solution! Thank you for helping me! So, let me provide you the information about the problem and all. In general I have an application for data analysis and forecast. GUI is written in C# that is supposed to help user to work with data. All the scripts therefore in R language. Yes, it might be not the best choice, but there's no way back. R.NET works not that great as it should. – alextheexplorerr May 26 '21 at 18:27
  • @GregorThomas, Speaking about the problem. There's a window that asks you to upload an excel file with data, then it displays the dataframe and its structure in datagrid. I need to provide data type changes if it's needed. Here is two comboboxes - one is for list of columns of dataframe and another is for data types. `args – alextheexplorerr May 26 '21 at 18:28
  • @GregorThomas, So, you gave me a hint to do this: `this_data – alextheexplorerr May 26 '21 at 18:29
  • Sorry, my typo, should be `assign(data, this_data)`. That will keep the same name. – Gregor Thomas May 26 '21 at 18:36
  • @GregorThomas, I meant to keep the same name as data list contains inside. It contains current file name. That's why I use `get` with it. I think I'll cut my functionality to only using 1 dataset per application run, so all datasets will be named 'data' only – alextheexplorerr May 26 '21 at 18:46
  • 1
    I understand, that's what `assign(data, this_data)` does. The first argument of `assign` is *"a variable name, given as a character string"*, just like first argument of `get` is *"an object name (given as a character string)"*. (From the `?assign` and `?get` help pages.) – Gregor Thomas May 26 '21 at 18:52
  • @GregorThomas, thank you so much! I will mention you as the co-author of the script for my graduate work! – alextheexplorerr May 26 '21 at 18:56
  • Lol, thanks or acknowledgement in the code is appreciated but I am not a co-author. – Gregor Thomas May 26 '21 at 19:21

0 Answers0