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

Related searches

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".

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"))

7 Modifying Values, 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 � Using these methods either you can replace a single cell or all the values of a row and column in a dataframe based on conditions .

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')

pandas.DataFrame.replace — pandas 1.1.2 documentation, You can replace multiple values at once as long as the number of new values will work whether you store your data in a vector, matrix, array, list, or data frame. If you compare objects of different data types, R will use its coercion rules to� The values from column V1(tbl_exl) can be found in the column Measured.mass(tbl_mz) and they should be replaced by the values from the next column m.zin tbl_mzdata frame. In another words the values in the V1should be replaced by the m.zvalues. The problem is that not all values from V1can't be find in the other data frame.

When you want to extract the year from the date, you can do this with the following line of code:

dataframe$year <- substr(dataframe$date,1,4)

When you want assign a class to the new variable simulataniously:

dataframe$year <- as.integer(substr(dataframe$date,1,4))

pandas.DataFrame.replace — pandas 1.1.1 documentation, str, regex and numeric rules apply as above. dict: Dicts can be used to specify different replacement values for different existing values. For example,� As you see in the picture, I have two columns. So, I want to replace the values smaller than 0.500000 from the column A_Freq with the values in the M.F column In the same rows. What is the best idea? for example: replace 0.312500 In the third row of the column A_Freq with 0.687500 From the same row but in the column M.F.

You can use match:

dataframe <- transform(dataframe, Year = year[match(date, yearend)])

      date variable value Year
1 19921231        a     1 1992
2 19931231        a     2 1993
3 19941231        a     3 1994
4 19941231        b     4 1994
5 19931231        b     5 1993
6 19941231        b     6 1994

Pandas How to replace values based on Conditions, Values of the DataFrame are replaced with other values dynamically. This differs from updating str, regex and numeric rules apply as above. dict: Dicts can be� replace replaces the values in x with indices given in list by those given in values . If necessary, the values in values are recycled.

Using these methods either you can replace a single cell or all the values of a row and column in a dataframe based on conditions . but what I want is how do you accomplish this in a functional way ,say with pacakage "purrr" (map,invoke_map functions)and does the job,in an elegant way for n such columns i.e replacing my categorical variables with some other values.

Suppose I have a 5*3 data frame in which third column contains missing value 1 2 3 4 5 NaN 7 8 9 3 2 NaN 5 6 NaN I hope to generate value for missing value based rule

I would like to change row names values from a first data frame if these rows names are present in another data frame, and change it to a corresponding value (defined in the second data frame).

Comments
  • This has been the answer that I was looking for!