Using R, I need help converting a non-numeric column into two numeric ones. I want to split the non-numeric data in column x, with the value before the dash going into one column (Start) and the value after the dash going into another column (End). Then, I want to create a new numeric column containing the difference between the Start and End columns with 1 added to the difference. (The Diff column contains a year count, so from 2011 to 2018 will be eight years.)
I encountered unexpected problems when I tried to do it. First, the x variable displayed as a factor. Second, the data in the Start and End columns were not numeric and when I tried to make them numeric so the Diff calculation could occur, I got a coercion error. Third, I could not get strsplit to work.
I checked stackoverflow solutions for comparable problems, but was unable to find one that presented a solution that worked for me.
The input data is just a very small sample of what is in the actual file
I would prefer a solution that uses dplyr, but am open to other ones.
Input
dput(df)
structure(list(x = c(NA, "1950-1960", "1975-1986", "2011-2018"
)), class = "data.frame", row.names = c(NA, -4L))
Output
x Start End Diff
1950-1960 1950 1960 11
1975-1986 1975 1986 12
2011-2018 2011 2018 8