I am having a big dataframe (dataset_n) consisting of several columns, each for a different variable. I am concentrating now on the variables:
- q32, i.e. net recalled wages
- pgssyear, i.e. the year when a person was asked the question about the wages
I would like to create an additional column that would stand for the CPI in a given year (pgssyear), so that I can calculate real wages later on (dividing the q32 with the CPI column).
I tried the following: - copying the pgssyear under a different name (creating a new vector with the name "year", but with the same contents) and replacing the first year: 1992 with 1 using "replace" hoping to be able to replace 1993 with e.g. 1.35, etc.:
attach(dataset_n)
dataset_n$year <- pgssyear
detach(dataset_n)
dataset_n$year
neither of the options: replace(dataset_n$year, dataset_n$year == 1992, 1) replace(dataset_n$year, dataset_n$year == "1992", 1) with(data.frame(dataset_n), replace(dataset_n$year, dataset_n$year == 1992, 1)))
worked for me. Each time I got the massage: "object of type 'closure' is not subsettable"
dataset_n$year[dataset_n$year==1992] <- 1
did not work either and I got the message:
Warning message:
In [<-.factor
(*tmp*
, dataset_n$year == 1992, value = c(NA, NA, :
invalid factor level, NA generated
I suspect that when creating the new vector the numeric data got treated as factors.
I tried also: as.numeric(gsub(1992, 1, dataset_n$year)) as.numeric(gsub(1993, 1.35, dataset_n$year))
This time the values got replaced, but I failed to achieve it "all at once", which is what I need.
I have also run out of further ideas, so any help would be appreciated. The other threads I have seen and which might be related are: Replace given value in vector Error in <my code> : object of type 'closure' is not subsettable