Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace values in data frame with other values according to a rule

Tags:

dataframe

r

map

I am an beginner in R and don`t find a solution for the following problem. Any help would be really appreciated!

I have a data.frame and want to replace certain values of a column with defined other values.

data.frame

date<-c("19921231","19931231","19941231","19941231","19931231","19941231")
variable<-c("a","a","a","b","b","b")
value<-c(1:6)
dataframe <- data.frame(date,variable,value)

attempt to solve problem

yearend<-c("19921231","19931231","19941231")
year<-c("1992","1993","1994")
map = setNames(yearend,year)
dataframe[] = map[dataframe]

error message

Error in map[dataframe] : invalid subscript type 'list'

The problem is obviously, that it is not a matrix. What is the most efficient way to solve this problem? It should also work if I want to replace "real" character, e.g. "BGSFDS" with "BASF stock".

like image 911
user3076270 Avatar asked Dec 13 '13 11:12

user3076270


People also ask

How do you replace values in a DataFrame with a condition?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.

How do you replace a specific value in a Pandas DataFrame?

DataFrame. replace() function is used to replace values in column (one value with another value on all columns). This method takes to_replace, value, inplace, limit, regex and method as parameters and returns a new DataFrame. When inplace=True is used, it replaces on existing DataFrame object and returns None value.


2 Answers

A nice function is mapvalues() from the plyr package:

require(plyr)
dataframe$newdate <- mapvalues(dataframe$date, 
          from=c("19921231","19931231","19941231"), 
          to=c("1992","1993","1994"))
like image 147
EDi Avatar answered Nov 02 '22 04:11

EDi


merge() might also be of help.

yearend<-c("19921231","19931231","19941231")
year<-c("1992","1993","1994")
map = data.frame(yearend,year)

merge(dataframe,map,by.x='date',by.y='yearend')
like image 2
Freddy Avatar answered Nov 02 '22 04:11

Freddy