How to merge create a column in R that only take one value based on update date

Ames

I am new R and have a matrix below:

earning

0  Name     usd/day  update_year  usd/day update_year
1  John     52.0     2011         NA       NA
2  Mary     44.0     2012         NA       NA
3  Nicole   44.5     2000 est.    49.2     2010 est.
4  Cynthia   38.1    2001 est.    44.0     2011

I wanted to clean up the data in R, to only have 3 columns - Name, usd/day and update_year and get something like this:

0  Name    usd/day  update_year
1  John    52.0     2011
2  Mary    44.0     2012
3  Nicole  49.2     2010
4  Cynthia 44.0     2011

How do I go about doing that in R please?

I don't want to manually combine them and the actual data is more than 100 rows.

milan

This should work. It seems that you want to extract the most recent date (i.e., year) and the highest value for USD. There's a couple of things you need to do.

First, only keep the year in update_year; it seems that you don't want ' est.' in your final table. We can do that using gsub.

df$update_year.x <- gsub("[^0-9]", "", df$update_year.x)
df$update_year.y <- gsub("[^0-9]", "", df$update_year.y)

Find the most recent year.

df$update_year <- apply(df[, c(4,6)], 1, max, na.rm=TRUE)

Find the highest USD value.

df$usd.day <- apply(df[, c(3,5)], 1, max, na.rm=TRUE)

Keep relevant columns.

df[, c("Name", "usd.day", "update_year")]

#     Name usd.day update_year
#1    John    52.0        2011
#2    Mary    44.0        2012
#3  Nicole    49.2        2010
#4 Cynthia    44.0        2011

Data

df <- read.table(text="
X0  Name     usd/day.x  update_year.x  usd/day.y update_year.y
1  John     52.0     2011         NA       NA
2  Mary     44.0     2012         NA       NA
3  Nicole   44.5     '2000 est.'    49.2     '2010 est.'
4  Cynthia   38.1    '2001 est.'    44.0     2011", header=TRUE,fill=TRUE,stringsAsFactors=FALSE)

As pointed out in the comments to your answer; there are duplicate column names and that's a problem. I solved that here by adding x/y at the end of the names.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R - Update a Value in One Column Based on Criteria in Other Tables

From Dev

how to get only one record based on column value in oracle join

From Dev

Update column value in XTS object based on date

From Dev

MySQL Update Value of column Based on DATE

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?

From Dev

How merge value of one cell with a whole column?

From Dev

How to update only month in date column?

From Dev

How do I make one column return a desired value based on the date in another column?

From Dev

How to partial update DataGrid - only for one column

From Dev

R create a column based on duplicate values of one column, and a second column

From Dev

How To Update The Date Based On Previous Record Value

From Dev

How To Update The Date Based On Previous Record Value

From Dev

R Create column based on previously observed value

From Dev

Create a column in one dataframe based on another column in another dataframe in R

From Dev

Creating new column based on earliest date value in other column in R

From Dev

Creating new column based on earliest date value in other column in R

From Dev

Update a Value in One Column Based on Criteria in Other Columns

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update column value from one position to another position based on indexed

From Dev

Update column in one table based on value in another table in mysql

From Dev

How to take only the date out of columns and copy the date to another column with another format of the date?

From Dev

Update only one column value in Access Database - VB

From Dev

r two dataframe merge by absolute value of one column

From Dev

r two dataframe merge by absolute value of one column

From Dev

Using SQL how can I update field values of one column to match a different column's field value only if the values do NOT equal?

From Dev

obtain corresponding value of one column based on minimum value of another in R

From Dev

How to create a new column based on conditions in R

From Dev

How to create a new column based on conditions in R

Related Related

  1. 1

    R - Update a Value in One Column Based on Criteria in Other Tables

  2. 2

    how to get only one record based on column value in oracle join

  3. 3

    Update column value in XTS object based on date

  4. 4

    MySQL Update Value of column Based on DATE

  5. 5

    UPDATE sql column with value from another column based on a date column

  6. 6

    How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?

  7. 7

    How merge value of one cell with a whole column?

  8. 8

    How to update only month in date column?

  9. 9

    How do I make one column return a desired value based on the date in another column?

  10. 10

    How to partial update DataGrid - only for one column

  11. 11

    R create a column based on duplicate values of one column, and a second column

  12. 12

    How To Update The Date Based On Previous Record Value

  13. 13

    How To Update The Date Based On Previous Record Value

  14. 14

    R Create column based on previously observed value

  15. 15

    Create a column in one dataframe based on another column in another dataframe in R

  16. 16

    Creating new column based on earliest date value in other column in R

  17. 17

    Creating new column based on earliest date value in other column in R

  18. 18

    Update a Value in One Column Based on Criteria in Other Columns

  19. 19

    Update column in one table based on value in another table in mysql

  20. 20

    Update column value from one position to another position based on indexed

  21. 21

    Update column in one table based on value in another table in mysql

  22. 22

    How to take only the date out of columns and copy the date to another column with another format of the date?

  23. 23

    Update only one column value in Access Database - VB

  24. 24

    r two dataframe merge by absolute value of one column

  25. 25

    r two dataframe merge by absolute value of one column

  26. 26

    Using SQL how can I update field values of one column to match a different column's field value only if the values do NOT equal?

  27. 27

    obtain corresponding value of one column based on minimum value of another in R

  28. 28

    How to create a new column based on conditions in R

  29. 29

    How to create a new column based on conditions in R

HotTag

Archive