Reputation: 57
I have the following column from my dataset:
HEIGHT
502
506
506
504
The first digit is feet and the last two are inches. How can I split this into two columns? I have tried split(), but it splits up every digit.
Feet Inches Total_Inches
5 02 62
5 06 66
5 06 66
5 04 64
Upvotes: 1
Views: 163
Reputation: 14370
I think a simple substring would work:
df$feet <- as.numeric(substr(df$HEIGHT,1,1))
df$inches <- as.numeric(substr(df$HEIGHT,2,nchar(df$HEIGHT)))
df$total_inches <- df$feet*12 + df$inches
df
# HEIGHT feet inches total_inches
#1 502 5 2 62
#2 506 5 6 66
#3 506 5 6 66
#4 504 5 4 64
Data:
df <- structure(list(HEIGHT = c(502L, 506L, 506L, 504L)), .Names = "HEIGHT", row.names = c(NA,
-4L), class = "data.frame")
Upvotes: 4