0

I have the following dataframe

df

ID Timestamp Package  Genre Name
1  01        com.abc  NA    NA
1  02        com.xyz  NA    NA
2  04        com.abc  NA    NA

Now the Package column has about 1000 unique packages on the basis of which I need to update the Genre and Name columns.

I know how to update these by using vectorized approach or by using within but this means I would have to iterate over all unique package names manually and I was hoping to find a sleeker solution.

Looking at switch function for column values and R Apply function depending on element in a vector, I was trying to make a switch function that could take in two arguements (package field and Genre field) and use switch statements to update. Not sure if it's the right way to go.

hbabbar
  • 947
  • 4
  • 15
  • 33
  • 1
    What is the source of your updates? Are you inputting them manually? Are they in another data structure, perhaps another table? You I would recommend a text editor or a spreadsheet program for manual editing. If you have the other data in a structure use `match` or `merge`, and see the R-FAQ [How to join data in R?](https://stackoverflow.com/q/1299871/903061) – Gregor Thomas Aug 22 '17 at 21:21

1 Answers1

0

Create a data.frame that contains the package information and merge them together on the package. First drop the genre and name columns as they will be populated with merge

df[, c("Genre", "Name")] <- NULL

df2 <- data.frame(Package = c("com.abc", "com.xyz"),
                  Genre = c("g1", "g2"),
                  Name = c("n1", "n2"))

merge(df, df2, by = "Package")

  Package ID Timestamp Genre Name
1 com.abc  1         1    g1   n1
2 com.abc  2         4    g1   n1
3 com.xyz  1         2    g2   n2
manotheshark
  • 4,297
  • 17
  • 30