如何使新列基于R中的一列从另一列AND行复制值



以下是一些示例数据:

data <- tibble(line_number = seq(1:5),
test = c("testA", "testB", "testC", "testD", "testE"),
start_date = as_date(c("2021-01-01", "2021-02-01", "2021-02-15", "2021-03-20", "2021-04-12")),
finish_date = as_date(c("2021-01-01", "2021-03-01", "2021-02-18", "2021-05-20", "2021-04-12")),
coded_date = c(NA, "1S", "2F", "2S", "4F"))
#       line_number test  start_date finish_date coded_date
#             <int> <chr> <date>     <date>      <chr>     
# 1               1 testA 2021-01-01 2021-01-01  NA        
# 2               2 testB 2021-02-01 2021-03-01  1S        
# 3               3 testC 2021-02-15 2021-02-18  2F        
# 4               4 testD 2021-03-20 2021-05-20  2S        
# 5               5 testE 2021-04-12 2021-04-12  4F        

我想创建两个名为";开始日期"以及";new_ finish_;其中其内容由";coded_ date";柱

对于coded_date列:数字对应于行号,";S〃;是指开始日期;F";表示完成日期。

以具有1S的行2为例;开始日期"列,以获取第1行中开始日期的值并复制它;new_ finish_;带有NA.

以具有2F的行3为例;new_ finish_;列,以获取第2行中的完成日期的值并复制它;开始日期"带有NA.

这是我想要的输出:

#       line_number test  start_date finish_date coded_date new_start_date new_finish_date
#             <int> <chr> <date>     <date>      <chr>      <date>         <date>
# 1               1 testA 2021-01-01 2021-01-01  NA         NA             NA
# 2               2 testB 2021-02-01 2021-03-01  1S         2021-01-01     NA
# 3               3 testC 2021-02-15 2021-02-18  2F         NA             2021-03-01
# 4               4 testD 2021-03-20 2021-05-20  2S         2021-02-01     NA
# 5               5 testE 2021-04-12 2021-04-12  4F         NA             2021-05-20

我还是一个使用R的初学者,所以任何帮助或输入都将不胜感激:(

我的第一直觉是使用两个用于循环

# deal with start date first
# get all values in 'coded_date' that contain an 'S'
svals <- grep(pattern = "S", x = data$coded_date, value = TRUE)
# we'll go row by row
# for each case (row) in that contains an 'S'
for(sval in svals){
# get the rowid from the value of 'coded_date'
# this is the row where we'll get the new date
rowid <- substring(text = sval, first = 1, last = 1)
# assign a 'new_start_date' to the row where we found sval
# the row containing this new value is defined by rowid
# use '%in% rather than '==' on left side because NAs are present
data[data$coded_date %in% sval, "new_start_date"] <- data[rowid,"start_date"]
}
## repeat for finish date
# S and F loops could be nested together!
fvals <- grep(pattern = "F", x = data$coded_date, value = TRUE)
for(fval in fvals){
rowid <- substring(text = fval, first = 1, last = 1)
data[data$coded_date %in% fval, "new_finish_date"] <- data[rowid,"finish_date"]

e: 这是一个矢量化的版本,似乎可以工作。如果有人感兴趣,我敢打赌这里还有改进的空间,我会感谢任何反馈!

data$new_start_date2 <- ifelse(
test = grepl(pattern = "S", x = data$coded_date),
yes = data[sub(pattern = "S", replacement = "", data$coded_date),"start_date"],
no = NA)
data$new_finish_date2 <- ifelse(
test = grepl(pattern = "F", x = data$coded_date),
yes = data[sub(pattern = "F", replacement = "", data$coded_date),"finish_date"],
no = NA)

相关内容

  • 没有找到相关文章

最新更新