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]] <- as.factor(get(data)[[columnName]])`, it will notify me that: `Error in get(data)[[columnName]] <- as.factor(get(data)[[columnName]]) : cannot find the "get<-"` function` – 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 <- get(data)`, modify it normally, `this_data[[columnName]] <- factor(this_data[[columnName]])`, and then assign it back with `assign(data, this_data)`. But overall this approach is not great---95% of the time people ask questions about `get` and `assign` and such there's a better overall approach. If you provide more context about your problem we can try to find a better way. – 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 <- commandArgs() #I receive arguments from the windows form` `data <- args[2] #A name of existing dataframe in the environment (e.g. NskData)` `columnName <- args[3] #A name of datagrid's column that requires data type change` `columnType <- args[4] #A needed data type` – alextheexplorerr May 26 '21 at 18:28
  • @GregorThomas, So, you gave me a hint to do this: `this_data <- get(data)` `this_data[[columnName]] <- as.factor(this_data[[columnName]])` `assign(get(data), this_data)` This is great! But is there any way to assign the data back to the origin data name? For example: I had 'KemData' file, then I changed some data types and saved them back to 'KemData'. I know it sounds hilarious, but... – 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