Jonsson Sali
Jonsson Sali

Reputation: 73

How to write results from text files after computations?

I have three text files . I want to do some calculations as shown below and write the results. All text files contain 14 columns X1 to X14 and 601 rows. This code basically calculate the correlations between (e.g. x1 from ref file and same column x1 from the file sour1 and so on) from all three files and do some computations and then return the results.

ref= read.table("D:\\S_asc.txt", sep="",header=TRUE)
sour1 = read.table("D:\\sre.txt", sep="",header=TRUE) 
sour2= read.table("D:\\os_asc.txt", sep="",header=TRUE)
columns <- paste0("X", 1:13)
lapply(
    columns,
    function(column)
    {
        result1 <- cor(ref[[column]],sour2[[column]],use = "na.or.complete")
        # calculate using ref and sour1

        result2 <-  cor(ref[[column]],sour1[[column]],use = "na.or.complete")   
        # calculate using ref and sour2

    }
)

I am getting error when writing the results:

write.table(result1,file = "foo.text")
Error in is.data.frame(x) : object 'result1' not found

sample of my file:

"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14"
"2" 0.189444444142282 0.317999997238318 0.166363635072202 0.301255410351666 0.471986238597248       0.307140744955515 0.411269603728529 0.262293613227113 0.265422601640182 0.196970316722191      0.123789306669182 0.291404434246697 0.322141939017999 0.43499182336885 0.327559809775301
"3" 0.149411764616768 0.191176471464774 0.180181817100807 0.422626723831494 0.496283823857852 0.327019521055943 0.441349119295007 0.286810015617714 0.24089.

        dput( head( ref ) ) 
          structure(list(X0 = c(0.0493461075417879, 0.0505162129561387, 
       0.0490028816673631, 0.049519460579677, 0.0496427079798826, 0.0506145435593228
       ), X1 = c(0.0262911450465596, 0.0249440745179165, 0.0240951294080254, 
      0.0256401352066387, 0.0258794329817874, 0.0247771567181071), 
        X2 = c(0.0426611743228151, 0.0374958657769128, 0.0306825108745032, 
         0.0393093163299325, 0.0398889994476742, 0.0435458658141826
         ), X3 = c(0.0954854469641096, 0.0833464493313783, 0.0790441582634745, 
         0.0818814458477848, 0.0772971938289144, 0.0818534809699011
        ), X4 = c(0.0933782886825547, 0.0968294484334356, 0.0981543002252867, 
         0.0969233013221434, 0.0933197039086707, 0.0913029715102785
         ), X5 = c(0.218212200747129, 0.220014530510402, 0.21109789420558, 
         0.204679954607016, 0.210967709615173, 0.20799168387628), 
       X6 = c(0.285405481705908, 0.284798691283541, 0.280772749846878, 
         0.285532926668878, 0.293501364933202, 0.296710869439616), 
        X7 = c(0.226218243796976, 0.216760771202585, 0.205519652752883, 
        0.206054283066245, 0.211842009508557, 0.214360803181363), 
       X8 = c(0.146648210899044, 0.144789395382814, 0.142059144367336, 
        0.141852209487122, 0.140823495101071, 0.140405076285803), 
        X9 = c(0.115716572518044, 0.113825119638324, 0.111749156097298, 
        0.111175507359272, 0.11094649408726, 0.111155298251533), 
         X10 = c(0.0675501818197432, 0.0636083677843573, 0.0605178392071445, 
           0.0549028963149205, 0.0600036757169919, 0.0625667586974292
         ), X11 = c(0.069120070466305, 0.0662362222295113, 0.0710726619403152, 
         0.0653345812188053, 0.0612221754441228, 0.0584222641456072
            ), X12 = c(0.281314574594234, 0.279000957729072, 0.276184085574267, 
            0.274121716868717, 0.271324697416127, 0.270268668362535), 
              X13 = c(0.364434947521643, 0.369388875815867, 0.372250445014838, 
          0.372470394613093, 0.372814994985087, 0.369151832740263), 
                X14 = c(0.0124844383491671, 0.0125512696973245, 0.0125538467586845, 
                   0.012599469429366, 0.0125682867660489, 0.0126515616426303
                   )), .Names = c("X0", "X1", "X2", "X3", "X4", "X5", "X6", 
                     "X7", "X8", "X9", "X10", "X11", "X12", "X13", "X14"), row.names = c("2", 
                     "3", "4", "5", "6", "7"), class = "data.frame")

Upvotes: 0

Views: 124

Answers (2)

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59970

You need to assign result1 to lapply.i.e.

result1 <- lapply( columns , function(column) { cor(ref[[column]],sour2[[column]],use = "na.or.complete") } )

And do the same for result 2:

result2 <- lapply( columns , function( column ) { cor(ref[[column]],sour1[[column]],use = "na.or.complete") } )

To separate out your results try altering your code to format your results like this...

    columns <- paste0("X", 1:13)
result1 <- lapply( columns , function(column){ cor(ref[[column]],sour2[[column]],use = "na.or.complete") } )
result2 <- lapply( columns , function(column){ cor(ref[[column]],sour1[[column]],use = "na.or.complete") } )
names( result1 ) <- columns
names( result2 ) <- columns


write.table( result1 , file = "foo.txt" , row.names = "res1" )
write.table( result2 , file = "foo.txt" , row.names = "res2" , col.names = FALSE  , append = TRUE )

Upvotes: 1

CHP
CHP

Reputation: 17189

You are getting that error because the variable created inside a function are out of scope outside that function i.e. when following function is evaluated, R doesn't know store values of result1 and result2 for use outside that function

function(column)
    {
        result1 <- cor(ref[[column]],sour2[[column]],use = "na.or.complete")
        # calculate using ref and sour1

        result2 <-  cor(ref[[column]],sour1[[column]],use = "na.or.complete")   
        # calculate using ref and sour2

    }

To demonstrate what I just said, try below code

testFunc <- function() {
    a <- 0
}

testFunc()

print(a)

You get error as follows :

Error in print(a) : object 'a' not found

Hence as suggested by SimonO101, you will have to assign result of lapply call for sour1 and sour2 individually.

result1 <- lapply( columns , function(column) { cor(ref[[column]],sour2[[column]],use = "na.or.complete") } )

result2 <- lapply( columns , function( column ) { cor(ref[[column]],sour1[[column]],use = "na.or.complete") } )

Upvotes: 2

Related Questions