0

I have 58 data named as: h5, h6,... h64. I want to run following script on all of them at one time and save the result in 1 new data file. this is my script:

t <- read.table("h5",header = F, row.names=1)
 cor(t)
 (cor(t))^2
lm(t$V3 ~ t$V2, data=t)

I know for reading all the data files at once I can use :

myFiles <- list.files(pattern="h.*")

But for the rest I do not know how to make a loop or how to store the result in a new data frame. any suggestion?

I show in below my input file structure :

HO840M3000540481 993.38 1543.1765
HO840M3000540483 -0.51 1120.8224
HO840M3000540497 1192.06 1651.3322
HO840M3004672697 388.5 1140.0917
HO840M3004672704 426.9 836.3676
HO840M3006005895 524.65 1301.8218
HO840M3006972816 1673.99 1389.1919
HO840M3006988837 188.35 385.6415
HO840M3006988891 2987.58 1919.2762
HO840M3006989113 1097.59 1746.4724
HO840M3007701817 1320.5 1058.4915
HO840M3007701858 -372.15 1090.6167
HO840M3007815980 548.58 759.3870
1
  • you can either check the total row count first and create an empty data.frame which you fill on each iteration or write/reload to a table with append write.table(x, file = "", append = TRUE) Commented Apr 13, 2017 at 6:18

1 Answer 1

1

Here's a shot in the dark...

myFiles <- paste0("h", 5:64)
all <- lapply(myFiles, read.table, header=F, row.names=1)

t1 <- lapply(all, cor)
t2 <- lapply(all, function(x) (cor(x))^2)

all.lm <- lapply(all, function(x)lm(V3 ~ V2, data=x))

EDIT: I can't reproduce the error mentioned in the comment, based on the data-structure provided. See below for the execution.

Please elaborate on what specific error you are facing.

dat <- structure(list(V2 = c(993.38, -0.51, 1192.06, 388.5, 426.9, 524.65, 
1673.99, 188.35, 2987.58, 1097.59, 1320.5, -372.15, 548.58), 
V3 = c(1543.1765, 1120.8224, 1651.3322, 1140.0917, 836.3676, 
1301.8218, 1389.1919, 385.6415, 1919.2762, 1746.4724, 1058.4915, 
1090.6167, 759.387)), .Names = c("V2", "V3"), class = "data.frame", row.names = c("HO840M3000540481", 
"HO840M3000540483", "HO840M3000540497", "HO840M3004672697", "HO840M3004672704", 
"HO840M3006005895", "HO840M3006972816", "HO840M3006988837", "HO840M3006988891", 
"HO840M3006989113", "HO840M3007701817", "HO840M3007701858", "HO840M3007815980"
))

all <- list(dat)


lapply(all, cor)


[[1]]
          V2        V3
V2 1.0000000 0.6741098
V3 0.6741098 1.0000000

lapply(all, function(x) (cor(x))^2)

[[1]]
          V2        V3
V2 1.0000000 0.4544241
V3 0.4544241 1.0000000


lapply(all, function(x)lm(V3 ~ V2, data=x))

[[1]]

Call:
lm(formula = V3 ~ V2, data = x)

Coefficients:
(Intercept)           V2  
   943.3531       0.3354  
Sign up to request clarification or add additional context in comments.

9 Comments

I get this error:Error in FUN(X[[i]], ...) : no lines available in input
Can't help you without knowing what your data structure is.
i put a part of data in my explanation to show you how is the data structure. meanwhile I do not have h17 and h33
thanks, but again the same error: all <- lapply(myFiles, read.table, header=F, row.names=1) Error in FUN(X[[i]], ...) : no lines available in input
Check your file names with list.files. Make sure the file name, path and extension are all correctly stated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.