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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.