Apply for loop or sapply on multiple columns in R
I have data like wherein A* represents the place with time format (01:01) as column names and the row names like 1C or 9D represent the individual ID.
structure(list(V1 = c("1C", "9D", "9F", "9H", "9S", "9T", "9Y"
), `A*01:01` = c(NA, NA, "1", NA, NA, NA, NA), `A*02:01` = c(NA,
NA, "1", NA, NA, NA, NA), `A*02:02` = c(NA, NA, "1", NA, NA,
NA, NA), `A*02:03` = c(NA, NA, "1", NA, NA, NA, NA), `A*02:05` = c(NA,
NA, NA, NA, NA, NA, "1"), `A*02:06` = c(NA, NA, NA, NA, NA, NA,
"1"), `A*03:01` = c(NA, NA, "1", NA, NA, NA, NA), `A*11:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `A*11:02` = c(NA, NA, NA, NA, NA, NA,
"1"), `A*23:01` = c(NA, NA, NA, NA, "1", NA, NA), `A*23:02` = c(NA,
NA, NA, NA, "1", NA, NA), `A*24:02` = c(NA, NA, NA, NA, "1",
NA, NA), `A*24:03` = c(NA, NA, NA, NA, "1", NA, NA), `A*25:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `A*26:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `A*29:01` = c(NA, NA, NA, NA, NA, "1", NA), `A*29:02` = c(NA,
NA, NA, NA, NA, "1", NA), `A*30:01` = c(NA, NA, NA, NA, "1",
NA, NA), `A*30:02` = c(NA, NA, NA, NA, "1", NA, NA), `A*31:01` = c(NA,
NA, NA, NA, NA, "1", NA), `A*32:01` = c(NA, NA, "1", NA, NA,
NA, NA), `A*33:01` = c(NA, NA, NA, NA, NA, "1", NA), `A*33:03` = c(NA,
NA, NA, NA, NA, "1", NA), `A*34:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `A*34:02` = c(NA, NA, NA, NA, NA, NA, "1"), `A*36:01` = c(NA,
NA, "1", NA, NA, NA, NA), `A*43:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `A*66:01` = c(NA, NA, NA, NA, NA, NA, "1"), `A*66:02` = c(NA,
NA, NA, NA, NA, NA, "1"), `A*68:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `A*68:02` = c(NA, NA, NA, NA, NA, NA, "1"), `A*69:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `A*74:01` = c(NA, NA, "1", NA, NA,
NA, NA), `A*80:01` = c(NA, NA, "1", NA, NA, NA, NA), `B*07:02` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*07:03` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*08:01` = c(NA, "1", NA, NA, NA, NA, NA), `B*13:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*13:02` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*14:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*14:02` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*14:05` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*14:06` = c(NA, NA, NA, NA, NA, NA, "1"), `B*15:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*15:02` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*15:03` = c(NA, NA, NA, NA, NA, NA, "1"), `B*15:10` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*15:11` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*15:12` = c(NA, NA, NA, NA, NA, NA, "1"), `B*15:13` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*15:16` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*15:18` = c(NA, NA, NA, NA, NA, NA, "1"), `B*18:01` = c(NA,
NA, NA, "1", NA, NA, NA), `B*27:03` = c(NA, NA, NA, "1", NA,
NA, NA), `B*27:05` = c(NA, NA, NA, "1", NA, NA, NA), `B*27:08` = c(NA,
NA, NA, "1", NA, NA, NA), `B*35:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*35:08` = c(NA, NA, NA, NA, NA, NA, "1"), `B*37:01` = c(NA,
NA, NA, "1", NA, NA, NA), `B*38:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*39:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*39:05` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*40:01` = c(NA, NA, NA, "1", NA,
NA, NA), `B*40:02` = c(NA, NA, NA, "1", NA, NA, NA), `B*40:05` = c(NA,
NA, NA, "1", NA, NA, NA), `B*40:06` = c(NA, NA, NA, "1", NA,
NA, NA), `B*41:01` = c(NA, NA, NA, "1", NA, NA, NA), `B*41:02` = c(NA,
NA, NA, "1", NA, NA, NA), `B*42:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*44:02` = c(NA, NA, NA, NA, NA, NA, "1"), `B*44:03` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*45:01` = c(NA, NA, NA, "1", NA,
NA, NA), `B*46:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*47:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*48:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*49:01` = c(NA, NA, NA, "1", NA, NA, NA), `B*50:01` = c(NA,
NA, NA, "1", NA, NA, NA), `B*51:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*51:02` = c(NA, NA, NA, NA, NA, NA, "1"), `B*52:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*53:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*54:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*55:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*56:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*57:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*57:03` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*58:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*59:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*67:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*73:01` = c(NA, NA, NA, "1", NA,
NA, NA), `B*78:01` = c(NA, NA, NA, NA, NA, NA, "1"), `B*81:01` = c(NA,
NA, NA, NA, NA, NA, "1"), `B*82:01` = c(NA, NA, NA, NA, NA, NA,
"1"), `B*82:02` = c(NA, NA, NA, NA, NA, NA, "1"), `C*01:02` = c("1",
NA, "1", NA, NA, NA, NA), `C*02:02` = c("1", NA, NA, NA, NA,
NA, "1"), `C*02:10` = c("1", NA, NA, NA, NA, NA, "1"), `C*03:02` = c(NA,
NA, NA, NA, NA, NA, "1"), `C*03:03` = c(NA, NA, NA, NA, NA, NA,
"1"), `C*03:04` = c(NA, NA, NA, NA, NA, NA, "1"), `C*04:01` = c(NA,
NA, NA, NA, "1", NA, NA), `C*04:03` = c(NA, NA, NA, NA, NA, NA,
"1"), `C*05:01` = c("1", NA, NA, NA, NA, NA, "1"), `C*06:02` = c("1",
"1", NA, NA, NA, NA, NA), `C*07:01` = c("1", "1", NA, NA, NA,
NA, NA), `C*07:02` = c("1", "1", NA, NA, NA, NA, NA), `C*07:04` = c("1",
"1", NA, NA, NA, NA, NA), `C*08:01` = c("1", NA, NA, NA, NA,
NA, "1"), `C*08:02` = c("1", NA, NA, NA, NA, NA, "1"), `C*12:02` = c("1",
NA, NA, NA, NA, NA, "1"), `C*12:03` = c("1", NA, NA, NA, NA,
NA, "1"), `C*14:02` = c("1", NA, NA, NA, "1", NA, NA), `C*15:02` = c("1",
NA, NA, NA, NA, NA, "1"), `C*16:01` = c("1", NA, NA, NA, NA,
NA, "1"), `C*17:01` = c(NA, NA, NA, NA, NA, NA, "1"), `C*18:01` = c("1",
"1", NA, NA, NA, NA, NA), `C*18:02` = c("1", "1", NA, NA, NA,
NA, NA)), row.names = c("1C", "9D", "9F", "9H", "9S", "9T", "9Y"
), class = "data.frame")
I am using the following code to process the data, which is working fine for column 2. But I have 16000 columns and 400 rows in my real data. I want to use "for loop" or "sapply" in R to perform processing. A quick solution is much appreciated.
LA <- dat[!is.na(dat[,2]),]
LA<-LA[,1]
res <- gsub("[[:digit:]]","",LA)
pos <- gsub("[[:alpha:]]","",LA)
LA_sep <- data.frame(res, pos)
LA_sep$res <- paste0(LA_sep$res, "&")
LA_sep$pos <- paste0(LA_sep$pos, "&")
LA_sep <- as.data.frame(t(LA_sep))
LA_sep <- apply(LA_sep[1:ncol(LA_sep)], 1, function(x) paste(na.omit(x), collapse = "")) ## nrow(ma) ## ncol(ma) ## NCOL(1:12) ## NROW(1:12)
write.csv(LA_sep, "2.csv")
When I run the above code on big data, I got the following output for 2nd column. So, it is very difficult to do this for each column manually or without a loop for big data. Every output should be saved as column names as a separate file.
res F&K&M&Q&E&R&A&R&N&A&N&N&H&N&M&H&S&A&N&T&N&N&G&T&G&T&T&TL&D&I&I&Y&F&R&D&M&I&K&K&R&A&H&H&A&H&A&R&R&R&G&D&G&P&I&E&L
pos 9&44&45&62&63&65&69&65&66&69&66&66&70&66&67&70&71&76&77&80&77&77&79&80&79&80&80&80&81&90&95&97&99&109&114&116&138&142&144&144&145&149&151&151&152&151&152&156&163&163&167&166&167&193&194&275&276
from Recent Questions - Stack Overflow https://ift.tt/3qurvnx
https://ift.tt/eA8V8J
Comments
Post a Comment