Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to find out the correlation between two data frames for 21 columns. I want the columns in 1 data frame to only be tested against their respective column in the other data frame. Kindly refer to the following code:

for(i in 4:24){

  for(x in 4:24){

    if(colnames(`Voss.TG5 1`)== colnames(`Voss.TG5 1`)) { cor.test(`Voss.TG5 1`[,i],`Voss.TG6 1`[,x]) }

      print(paste(colnames(`Voss.TG5 1`)[i], "est:",a$estimate,"p=value",a$p.value))

  }

}

The error message:

the condition has length >1 and only the first element will be used

1 Answer

0 votes
by (107k points)

I am assuming that you want to compare column number 4 of the first data.frame with column 4 of a second data.frame, and 5 with 5, etc., then in that case, you only need one value to loop over.

Say for instance:

set.seed(123)

df1 <- data.frame(matrix(rnorm(24e3), nrow=100, ncol=24), 

                  row.names = paste0("id_", 1:100))

colnames(df1) <- paste0("col_", 1:24)

set.seed(456)

df2 <- data.frame(matrix(rnorm(24e3), nrow=100, ncol=24), 

                  row.names = paste0("id_", 1:100))

colnames(df2) <- paste0("col_", 1:24)

getCorResult <- function(x) {

    ct <- cor.test(df1[, x], df2[, x])

    paste0(colnames(df1)[x], " est: ", ct$estimate,"; p-value: ", ct$p.value)

}

for (i in 4:24) print(getCorResult(i))

#> [1] "col_4 est: 0.127978798171234; p-value: 0.204468537818308"

#> [1] "col_5 est: -0.130168697527086; p-value: 0.19676568528598"

#> [1] "col_6 est: -0.00401742175033025; p-value: 0.968356791558258"

#> [1] "col_7 est: -0.13911136442779; p-value: 0.167480254977386"

#> [1] "col_8 est: -0.0802509291976723; p-value: 0.427369490913814"

#> [1] "col_9 est: 0.0336457448519071; p-value: 0.739646539780128"

#> [1] "col_10 est: 0.101054217409236; p-value: 0.317116556319783"

#> [1] "col_11 est: -0.16499181273189; p-value: 0.100914127184667"

#> [1] "col_12 est: 0.0883842999330604; p-value: 0.381876567737707"

#> [1] "col_13 est: 0.133978968109195; p-value: 0.183865978193586"

#> [1] "col_14 est: -0.146591444752327; p-value: 0.145569923059057"

#> [1] "col_15 est: -0.0994068480087866; p-value: 0.325110357585432"

#> [1] "col_16 est: 0.0510291962770492; p-value: 0.614117314454403"

#> [1] "col_17 est: -0.221875409962651; p-value: 0.0265146668160414"

#> [1] "col_18 est: 0.0272800890564412; p-value: 0.787605537795977"

#> [1] "col_19 est: 0.0480880241589448; p-value: 0.63470899715546"

#> [1] "col_20 est: 0.0178734013379405; p-value: 0.859900388976822"

#> [1] "col_21 est: 0.0419569274347847; p-value: 0.678524920388683"

#> [1] "col_22 est: -0.0790894327800203; p-value: 0.43411046999303"

#> [1] "col_23 est: -0.0222165574940321; p-value: 0.826338882900991"

#> [1] "col_24 est: -0.184979303595513; p-value: 0.0654062507984986"

Learn R programming from scratch if you are a beginner and want to know more about R language. 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...