I have a dataset called 'names' as shown below. The 'expected.entry.in.this.col' column is currently empty, but below I have shown how it should look. How can I write the logic?
Basically I think I'll need to run a loop through every row and for each row, use an 'if' condition to check the format and then enter the data into 'expected.entry.in.this.col' appropriately. How would I go about doing this? (a bit unfamiliar with R syntax for these kind of tasks).
names
EDIT: row 3 is a mistake and should read williams.harry
try something like this:
df <- data.frame(first = c("Kevin", "Megan"), last = c("Spacey", "Fox"),
format = c("f.last", "F.L."))
df$new <- NA
df$new <- ifelse(df$format == "f.last",
tolower(paste0(substr(df$first,1,1),".",df$last)),
df$new)
df$new <- ifelse(df$format == "F.L.",
paste0(substr(df$first,1,1),".", substr(df$last,1,1)),
df$new)
df
first last format new
1 Kevin Spacey f.last k.spacey
2 Megan Fox F.L. M.F