0

Based on this link, I wrote the following code, which is part of a function:

Sample data:

panelID = c(1:50)   
year= c(2001:2010)
country = c("NLD", "BEL", "GER")
urban = c("A", "B", "C")
indust = c("D", "E", "F")
sizes = c(1,2,3,4,5)
n <- 2
library(data.table)
set.seed(123)
DT <- data.table(panelID = rep(sample(panelID), each = n),
                    country = rep(sample(country, length(panelID), replace = T), each = n),
                    year = c(replicate(length(panelID), sample(year, n))),
                    some_NA = sample(0:5, 6),                                             
                    Factor = sample(0:5, 6), 
                    industry = rep(sample(indust, length(panelID), replace = T), each = n),
                    urbanisation = rep(sample(urban, length(panelID), replace = T), each = n),
                    size = rep(sample(sizes, length(panelID), replace = T), each = n),
                    income = round(runif(100)/10,2),
                    sales= round(rnorm(10,10,10),2),
                    happiness = sample(10,10),
                    Sex = round(rnorm(10,0.75,0.3),2),
                    Age = sample(100,100),
                    educ = round(rnorm(10,0.75,0.3),2))        
DT [, uniqueID := .I]                                                         # Creates a unique ID     
DT <- as.data.frame(DT)

Code:

depvar <- "happiness"
othervar <- "factor:income"
insvar <- c("happiness","factor","income")

  if (length(insvar)>2) {
    DT$newvar <- DT[insvar[2]]*DT[insvar[3]]
    othervar=newvar
  }

The idea is that when othervar is a combination of two variables, othervar gets replaced by a new variable which is the combination of those two variables.

Right now I however get the error:

Error in `[.data.frame`(DT, insvar[2]) : undefined columns selected

How should I write this function properly?

Tom
  • 2,173
  • 1
  • 17
  • 44

1 Answers1

1

If you change factor to Factor as the column is named and use DT$newvar the code runs and produces a new column, which I believe is what you are looking for.

depvar <- "happiness"
othervar <- "Factor:income"
insvar <- c("happiness","Factor","income")

if (length(insvar)>2) {
  DT$newvar <- DT[insvar[2]]*DT[insvar[3]]
  othervar=DT$newvar
}
MarBlo
  • 4,195
  • 1
  • 13
  • 27
  • Thanks for pointing out my mistake! For some reason I always till assume that my code does not work instead of that I made a mistake when trying something new. I guess I'll delete the question as it is based on a typo. – Tom Jun 11 '20 at 09:27
  • @Tom, OK, up to you. I am glad it helped – MarBlo Jun 11 '20 at 09:30