0

I am having a big dataframe (dataset_n) consisting of several columns, each for a different variable. I am concentrating now on the variables:

  1. q32, i.e. net recalled wages
  2. 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

Community
  • 1
  • 1
Asiack
  • 47
  • 8

1 Answers1

0

To make this line work:

dataset_n$year[dataset_n$year==1992] <- 1

Convert your year vector to numeric from factor like this:

dataset_n$year <- as.numeric(as.character(dataset_n$year))
Nick DiQuattro
  • 729
  • 4
  • 7
  • At first i got NAs instead, but after restarting R, it worked! Thank you. Any ideas about the other options welcome :) – Asiack Nov 30 '14 at 21:30
  • @Asiack You could try them again after doing the conversion to numeric, they might work then as well. – Nick DiQuattro Nov 30 '14 at 21:41