0

Preface: I have seen this post:How to convert a factor to an integer\numeric without a loss of information? , but it does not really apply to the issue I am having. It addresses the issue of converting a vector in the form of factor to a numeric, but the issue I am having is larger than that.

Problem: I am trying to convert a column in a dataframe from a factor to a numeric, while representing the dataframe using paste0. Here is an example:

aa=1:10
bb=rnorm(10)
dd=data.frame(aa,bb)
get(paste0("d","d"))[,2]=as.factor(get(paste0("d","d"))[,2])

(The actual code I am using requires me to use the paste0 function) I get the error: target of assignment expands to non-language object I am not sure how to do this, I think what is messing it up is the paste0 function.

Community
  • 1
  • 1
naveace
  • 65
  • 1
  • 8
  • You cannot have `get` on the `lhs` of `=`. It is not clear what you want to do, Perhaps `assign` – akrun Jul 15 '16 at 15:10
  • @akrun I tried assign(paste0("d","d')[,2],as.factor(paste0("d","d')[,2])). That gives me the error: Incorrect number of dimensions – naveace Jul 15 '16 at 15:22
  • 1
    My standard response to this problem is that you've programmed yourself into a corner by convincing yourself that it is "necessary" to use `get` or `assign`, and the solution is to restructure your code to use named lists. With named lists, referring to objects programmatically via characters is a breeze. – joran Jul 15 '16 at 15:24
  • @joran could you expand on that? How would you do that for this kind of example? – naveace Jul 15 '16 at 15:24
  • If `dd` were in an object like `mylist = list(dd = dd)`, then you can always retrieve it via `mylist[["dd"]]`, and columns like `mylist[["dd"]][,2]`, _and_ assign to them that way simply using `<-`. – joran Jul 15 '16 at 15:27

1 Answers1

0

First, this is not really a natural way to think about things or to code things in R. It can be done, but if you rephrase your question to give the bigger picture, someone can probably provide more natural ways of doing this in R. (Like the named lists @joran mentioned in the comment.)

With that said, to do this in R, you need to split apart the three steps you're trying to do in one line: get the data frame with the specified variable, make the desired column a factor, and then assign back to the variable name. Here I've wrapped this in a function, so the assignment needs to be made in pos=1 instead of the default, which would name it only within the function.

tof <- function(dfname, colnum) {
    d <- get(dfname)
    d[, colnum] <- factor(d[, colnum])
    assign(dfname, d, pos=1)
}

dd <- data.frame(aa=1:10, bb=rnorm(10))
str(dd)
## 'data.frame':    10 obs. of  2 variables:
##  $ aa: int  1 2 3 4 5 6 7 8 9 10
##  $ bb: num  -1.4824 0.7904 0.0258 1.2075 0.2455 ...

tof("dd", 2)
str(dd)
## 'data.frame':    10 obs. of  2 variables:
##  $ aa: int  1 2 3 4 5 6 7 8 9 10
##  $ bb: Factor w/ 10 levels "-1.48237228248052",..: 1 8 4 9 5 10 2 7 3 6
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142