I checked the previous post on how to convert lapply output to data frame which did not help me
I have asked two questions, I tried to give a representative data but it is not. the solution works on example but not on the real data . this is my question.
how to apply a function on every column of a data?
The problem which made me crazy and I cannot solve it is
I can do that and it works perfectly on the data but I must do that 1000 times and generate 1000 files....
s1 <- normalize(df[,1], ";")
Mn <- as.data.frame(process(s1))
write.table(Mn, file= "~/Desktop/outputs/output62.txt", quote = FALSE, sep="\t")
rm(Mn,s1)
but when I use
lapply(myS, process)
I get error like:
Error in data.frame(All_Fractions = c(161L, 153L, 218L, 2847L, 2565L, : arguments imply differing number of rows: 7, 5, 10
I have no choice but post my real data in order to be able to solve my problem.
I load them like this
df1 <- read.table("~/Desktop/df1.txt", sep="\t", header=TRUE, stringsAsFactors=FALSE)
df2 <- read.table("~/Desktop/df2.txt", sep="\t", header=TRUE, stringsAsFactors=FALSE)
This is the code I have used so far
normalize <- function(x, delim) {
x <- gsub(")", "", x, fixed=TRUE)
x <- gsub("(", "", x, fixed=TRUE)
idx <- rep(seq_len(length(x)), times=nchar(gsub(sprintf("[^%s]",delim), "",
as.character(x)))+1)
names <- unlist(strsplit(as.character(x), delim))
return(setNames(idx, names))
}
myS <- lapply(df1, normalize,";")
lookup <- normalize(df2[,1], ",")
process <- function(s) {
lookup_try <- lookup[names(s)]
found <- which(!is.na(lookup_try))
pos <- lookup_try[names(s)[found]]
return(paste(pos, sep=""))
}
for output I tried this
Mn <- as.data.frame(lapply(myS, process),FUN=as.data.frame)
gives me error
Error in data.frame(Fraction_1 = c(393L, 674L, 79L, 2447L, 248L), Fraction_2 = c(2107L, : arguments imply differing number of rows: 5, 30, 51, 35
I tried
Mn <- as.data.frame(lapply(myS, process))
Error in data.frame(Fraction_1 = c(393L, 674L, 79L, 2447L, 248L), Fraction_2 = c(2107L, : arguments imply differing number of rows: 5, 30, 51, 35
gives me error
Mn <- lapply(myS, process)
I cannot save the output
write.table(Mn, file= "~/Desktop/outputs/output.txt", quote = FALSE, sep="\t")
Error in data.frame(Fraction_1 = c(393L, 674L, 79L, 2447L, 248L), Fraction_2 = c(2107L, : arguments imply differing number of rows: 5, 30, 51, 35