Reputation: 397
I have questions regarding the transformation of a column value (C1 column) to several other values, as follows:
C1 = c("280804-6759","180604-8084")
C2 = c("280804","180604")
C3 = c("28.08.04","18.06.04")
C4 = c("28-08-04","18-06-04")
C5 = c(0,1)
df = data.frame(C1, C2, C3, C4,C5)
C1 C2 C3 C4 C5
1 280804-6759 280804 28.08.04 28-08-04 0
2 180604-8084 180604 18.06.04 18-06-04 1
Hope you can help. Thanks in advance.
Sincerily, ykl
Upvotes: 1
Views: 62
Reputation: 193507
In base R, you can use within
. Below, I assume "df" is just the first column in your sample data.
df <- data.frame(C1)
within(df, {
C2 <- gsub("-.*$", "", C1)
C3 <- gsub("(..)(..)(..)", "\\1.\\2.\\3", C2)
C4 <- gsub("\\.", "-", C3)
C5 <- as.numeric(grepl("[02468]$", C1))
})[paste("C", 1:5, sep = "")]
# C1 C2 C3 C4 C5
# 1 280804-6759 280804 28.08.04 28-08-04 0
# 2 180604-8084 180604 18.06.04 18-06-04 1
Same approach, but with "dplyr":
library(dplyr)
df %>%
mutate(C2 = gsub("-.*$", "", C1),
C3 = gsub("(..)(..)(..)", "\\1.\\2.\\3", C2),
C4 = gsub("\\.", "-", C3),
C5 = as.numeric(grepl("[02468]$", C1)))
Upvotes: 2