Reputation: 99
I haven't used R in a while and I got the opportunity to practice with a new public dataset. However, the mutate
function is not behaving the way I am expecting it to and I am confused because this is a supposed to be an easy operation. Here is a sample of the data:
structure(list(LName = c("DENTON", "CHAMBERS", "BURANDT", "ROTHMAN", "ROSENBERG"),
Birthdate = c("02/07/1962", "02/20/1942", "09/16/1958",
"03/28/1994", "04/20/1986"),
Gender = c("F", "F", "F", "F", "F"),
PrecinctCode = c(2323L, 2300L, 2341L, 2263L, 1365L),
LegislativeDistrict = c(46L, 46L, 46L, 46L, 43L),
Registrationdate = c("02/14/1992", "10/03/1990", "05/20/1984", "08/14/2012", "08/11/2015"),
LastVoted = c("11/08/2016", "11/04/2008", "02/12/2019", "11/08/2016", "11/06/2018"),
StatusCode = c("I", "I", "A", "A", "A")),
.Names = c("LName", "Birthdate", "Gender", "PrecinctCode",
"LegislativeDistrict", "Registrationdate", "LastVoted", "StatusCode"),
row.names = c(NA, -5L),
class = c("tbl_df", "tbl", "data.frame"))
What I am trying to do is create a new column using mutate and populating it with data and strings to form a particular output. The output should read as SEA 43-1365
The code I am using is:
sample_data %>%
mutate("SEA" & " " & LegislativeDistrict & "-" & PrecinctCode)
The error I am getting is this:
Error in mutate_impl(.data, dots) :
Evaluation error: operations are possible only for numeric, logical or complex types.
I have tried changing the data formats of some of the columns, I have also tried using the paste
function. I am kinda stumped regarding what I am doing wrong but I know that it's going to be painfully obvious when someone points it out.
Thank you!
Upvotes: 0
Views: 62
Reputation: 11981
try this:
sample_data %>%
mutate(newcol = paste("SEA", LegislativeDistrict, "-", PrecinctCode, sep = " "))
Upvotes: 3