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".
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.
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.
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"))
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With