在R中的数据帧中自动填充列;坐标;

在R中的数据帧中自动填充列;坐标;,r,dataframe,R,Dataframe,从数据帧y开始: x <- c(2,NA,6,8,9,10) y <- data.frame(letters[1:6], 1:6, NA, 3:8, NA, x, NA) colnames(y) <- c("Patient", "C1", "First_C1", "C2", "First_C2", "C3", "First_C3") x我们从y开始,没有输出列: y<-structure(list(Patient = structure(1:6, .Label = c(

从数据帧y开始:

x <- c(2,NA,6,8,9,10)
y <- data.frame(letters[1:6], 1:6, NA, 3:8, NA, x, NA)
colnames(y) <- c("Patient", "C1", "First_C1", "C2", "First_C2", "C3", "First_C3")

x我们从
y
开始,没有输出列:

y<-structure(list(Patient = structure(1:6, .Label = c("a", "b", 
"c", "d", "e", "f"), class = "factor"), C1 = 1:6, C2 = 3:8, C3 = c(2, 
NA, 6, 8, 9, 10)), .Names = c("Patient", "C1", "C2", "C3"), row.names = c(NA, 
-6L), class = "data.frame")

y我们从
y
开始,没有输出列:

y<-structure(list(Patient = structure(1:6, .Label = c("a", "b", 
"c", "d", "e", "f"), class = "factor"), C1 = 1:6, C2 = 3:8, C3 = c(2, 
NA, 6, 8, 9, 10)), .Names = c("Patient", "C1", "C2", "C3"), row.names = c(NA, 
-6L), class = "data.frame")

yIt根据输出不清楚根据输出不清楚太好了!这就是我想要的。但是如果第2:4列是日期怎么办?你将如何更改代码?太好了!这就是我想要的。但是如果第2:4列是日期怎么办?您将如何更改代码?
y[paste0("First_C",1:3)]<-lapply(y[,2:4],
      function(x) {
         d<-arrayInd(match(x,t(y[,2:4])),dim(t(y[,2:4])))[,2:1]
         paste(y$Patient[d[,1]],colnames(y[,2:4])[d[,2]],sep="_")
})
y[,5:7][is.na(y[,2:4])]<-NA
#  Patient C1 C2 C3 First_C1 First_C2 First_C3
#1       a  1  3  2     a_C1     a_C2     a_C3
#2       b  2  4 NA     a_C3     b_C2     <NA>
#3       c  3  5  6     a_C2     c_C2     c_C3
#4       d  4  6  8     b_C2     c_C3     d_C3
#5       e  5  7  9     c_C2     e_C2     e_C3
#6       f  6  8 10     c_C3     d_C3     f_C3