How to format the output in CSV file when outputting - r

So I am trying to get an output in csv file but I am having trouble formatting as per my need.
My Code
method.metric <- mmetric(testCenScal[[course_name]], method.pred, c("RMSE", "R2", "MAE", "COR"))
write.table(method.metric, "metric.csv", sep = ",", col.names = T, append = T)
Current Output
"x"
"MAE",0.636059658390333
"RMSE",0.814405873704867
"COR",0.581863604936215
"R2",0.338565254749368
"x"
"MAE",0.636059658390333
"RMSE",0.814405873704867
"COR",0.581863604936215
"R2",0.338565254749368
"x"
"RMSE",0.869309100173694
"R2",0.356594555638249
"MAE",0.653084184175849
"COR",0.597155386510286
"x"
"RMSE",0.869309100173694
"R2",0.356594555638249
"MAE",0.653084184175849
"COR",0.597155386510286
It would be nice if I could format this output into something like:
RMSE R2 MAE COR param1 param2
0.89 0.35 0.65 0.59 courseA Blackboost
0.89 0.35 0.65 0.59 courseB Blackboost
0.89 0.35 0.65 0.59 courseC Blackboost
0.89 0.35 0.65 0.59 courseD Blackboost
0.89 0.35 0.65 0.59 courseE Blackboost
0.89 0.35 0.65 0.59 courseA Rpart
0.89 0.35 0.65 0.59 courseB Rpart
0.89 0.35 0.65 0.59 courseC Rpart
0.89 0.35 0.65 0.59 courseD Rpart
0.89 0.35 0.65 0.59 courseE Rpart
I dont know what is "x" and where is it coming from, I guess I don't have the column name mentioned therefore it prints default as "x"?
I have this code in a function so I am passing two parameters one is the method and another is the target field. I would like to print those while appending it to a CSV file.
If I type dput(method.metric)
I get output as:
structure(c(0.869309100173694, 0.356594555638249, 0.653084184175849,
0.597155386510286), .Names = c("RMSE", "R2", "MAE", "COR"))
I already tried using the code write.csv(method.metric, file ="metric.csv", row.names=FALSE, eol=",", append=T) but it did not help much.
I will try to work on what you said formatting in R using cbind and other functions. If I get the output in above format, I will be able to create graphs with ease as I have lot of predictive model results being output.

Related

Obtain standardized loadings ("pattern matrix") from psych::fa object

The psych::print.psych() function produces beautiful output for the factor analysis objects produced by psych::fa(). I would like to obtain the table that follows the text "Standardized loadings (pattern matrix) based upon correlation matrix" as a data frame without cutting and pasting.
library(psych)
my.fa <- fa(Harman74.cor$cov, 4)
my.fa #Equivalent to print.psych(my.fa)
Yields the following (I'm showing the first four items here):
Factor Analysis using method = minres
Call: fa(r = Harman74.cor$cov, nfactors = 4)
Standardized loadings (pattern matrix) based upon correlation matrix
MR1 MR3 MR2 MR4 h2 u2 com
VisualPerception 0.04 0.69 0.04 0.06 0.55 0.45 1.0
Cubes 0.05 0.46 -0.02 0.01 0.23 0.77 1.0
PaperFormBoard 0.09 0.54 -0.15 0.06 0.34 0.66 1.2
Flags 0.18 0.52 -0.04 -0.02 0.35 0.65 1.2
I tried examining the source code for print.psych (Using View(print.psych) in RStudio), but could only find a section for printing standardized loadings for 'Factor analysis by Groups'.
The my.fa$weights are not standardized, and the table is missing the h2, u2, and com columns. If they can be standardized, the following code could work:
library(data.table)
library(psych)
my.fa <- fa(Harman74.cor$cov,4)
my.fa.table <- data.table(dimnames(Harman74.cor$cov)[[1]],
my.fa$weights, my.fa$communalities, my.fa$uniquenesses, my.fa$complexity)
setnames(my.fa.table, old = c("V1", "V3", "V4", "V5"),
new = c("item", "h2", "u2", "com"))
Printing my.fa.table gives the following (I show the first four lines), which indicates $weights is incorrect:
item MR1 MR3 MR2 MR4 h2 u2 com
1: VisualPerception -0.021000973 0.28028576 0.006002429 -0.001855021 0.5501829 0.4498201 1.028593
2: Cubes -0.003545975 0.11022570 -0.009545919 -0.012565221 0.2298420 0.7701563 1.033828
3: PaperFormBoard 0.028562047 0.13244895 -0.019162262 0.014448449 0.3384722 0.6615293 1.224154
4: Flags 0.009187032 0.14430196 -0.025374834 -0.033737089 0.3497962 0.6502043 1.246102
Replacing $weights with $loadings gives the following error message:
Error in as.data.frame.default(x, ...) :
cannot coerce class ‘"loadings"’ to a data.frame
Update:
Adding [,] fixed the class issue:
library(data.table)
library(psych)
my.fa <- fa(Harman74.cor$cov,4)
my.fa.table <- data.table(dimnames(Harman74.cor$cov)[[1]],
my.fa$loadings[,], my.fa$communalities, my.fa$uniquenesses, my.fa$complexity)
setnames(my.fa.table, old = c("V1", "V3", "V4", "V5"),
new = c("item", "h2", "u2", "com"))
my.fa.table
item MR1 MR3 MR2 MR4 h2 u2 com
1: VisualPerception 0.04224875 0.686002901 0.041831185 0.05624303 0.5501829 0.4498201 1.028593
2: Cubes 0.05309628 0.455343417 -0.022143990 0.01372376 0.2298420 0.7701563 1.033828
3: PaperFormBoard 0.08733001 0.543848733 -0.147686005 0.05523805 0.3384722 0.6615293 1.224154
4: Flags 0.17641395 0.517235582 -0.038878915 -0.02229273 0.3497962 0.6502043 1.246102
I would still be happy to get an answer that does this more elegantly or explains why this isn't built in.
It is not built in because each person wants something slightly different. As you discovered, you can create a table by combining four objects from fa: the loadings, the communalities, the uniqueness, and the complexity.
df <- data.frame(unclass(f$loadings), h2=f$communalities, u2= f$uniqueness,com=f$complexity)
round(df,2)
so, for the Thurstone correlation matrix:
f <- fa(Thurstone,3)
df <- data.frame(unclass(f$loadings), h2=f$communalities, u2= f$uniqueness,com=f$complexity)
round(df,2)
Produces
MR1 MR2 MR3 h2 u2 com
Sentences 0.90 -0.03 0.04 0.82 0.18 1.01
Vocabulary 0.89 0.06 -0.03 0.84 0.16 1.01
Sent.Completion 0.84 0.03 0.00 0.74 0.26 1.00
First.Letters 0.00 0.85 0.00 0.73 0.27 1.00
Four.Letter.Words -0.02 0.75 0.10 0.63 0.37 1.04
Suffixes 0.18 0.63 -0.08 0.50 0.50 1.20
Letter.Series 0.03 -0.01 0.84 0.73 0.27 1.00
Pedigrees 0.38 -0.05 0.46 0.51 0.49 1.96
Letter.Group -0.06 0.21 0.63 0.52 0.48 1.25
Or, you can try the fa2latex for nice LaTex based formatting.
fa2latex(f)
which produces a LateX table in quasi APA style.

Unable to tweak the findAssocs() in tm package in R

I was trying to find associations between top 10 frequent words with the rest of the frequent words int the input text.
When I look at the individual output of findAssocs():
findAssocs(dtm, "good", corlimit=0.4)
It gives the output clearly by printing the word 'good' with which associations have been sought.
$good
better got hook next content fit person
0.44 0.44 0.44 0.44 0.43 0.43 0.43
But when I try to automate this process for a character vector having top 10 words:
t10 <- c("busi", "entertain", "topic", "interact", "track", "content", "paper", "media", "game", "good")
the output is a list of correlations for each of those elements BUT WITHOUT THE WORD WITH WHICH THE ASSOCIATIONS HAVE BEEN SOUGHT. The sample output is as below (plz notice that the word at t10[i] is not printed, unlike the above individual output where 'good' was clearly printed):
for(i in 1:10) {
t10_words[i] <- as.list(findAssocs(dtm, t10[i], corlimit=0.4))
}
> t10_words
[[1]]
littl descript disrupt enter model
0.50 0.48 0.48 0.48 0.48
[[2]]
immers anyth effect full holodeck iot problem say startrek such suspect wow
0.68 0.48 0.48 0.48 0.48 0.48 0.48 0.48 0.48 0.48 0.48 0.48
[[3]]
area captur give overal like alon avid begin
0.51 0.47 0.47 0.47 0.44 0.43 0.43 0.43
circuit cloud collaboration communic communiti concis confus defin
0.43 0.43 0.43 0.43 0.43 0.43 0.43 0.43
discord doesnt drop enablesupport esport event everi everyon
0.43 0.43 0.43 0.43 0.43 0.43 0.43 0.43
How do I print the output along with the actual association word?
Can somebody please help me with this??
Thanks.
After running your for loop, add the following piece of code:
names(t10_words) <- t10
This will name the lists with the words specified in t10.

Save unequal output to a csv or txt file

I want to save the following output I get in the R console into a csv or txt file.
Discordancy measures (critical value 3.00)
0.17 3.40 1.38 0.90 1.62 0.13 0.15 1.69 0.34 0.39 0.36 0.68 0.39
0.54 0.70 0.70 0.79 2.08 1.14 1.23 0.60 2.00 1.81 0.77 0.35 0.15
1.55 0.78 2.87 0.34
Heterogeneity measures (based on 100 simulations)
30.86 14.23 3.75
Goodness-of-fit measures (based on 100 simulations)
glo gev gno pe3 gpa
-3.72 -12.81 -19.80 -32.06 -37.66
This is the outcome I get when I run the following
Heter<-regtst(regsamlmu(-extremes), nsim=100)
where Heter is a list (i.e., is.list(Heter) returns TRUE)
You could use capture.output:
capture.output(regtst(regsamlmu(-extremes), nsim=100), file="myoutput.txt")
Or for capturing output coming from several consequential commands:
sink("myfile.txt")
#
# [commands generating desired output]
#
sink()
You could make a character vector which you write to a file. Each entry in the vector will be separated by a newline character.
out <- capture.output(regtst(regsamlmu(-extremes), nsim=100))
write(out, "output.txt", sep="\n")
If you would like to add more lines just do something like c(out, "hello Kostas")

R rownames(foo[bar]) prints as null but can be successfully changed - why?

I've written a script that works on a set gene-expression data.
I'll try to separate my post in the short question and the rather lengthy explanation (sorry about that long text block). I hope the short question makes sense in itself. The long explanation is simply to clarify if I don't get the point along in the short question.
I tried to aquire basic R skills and something that puzzles me occurred, and I didn't find any enlightment via google. I really don't understand this. I hope that by clarifying what is happening here I can better understand R.
That said I'm not a programmer so please bear with my bad code.
SHORT QUESTION:
When I have rownames(foo) e.g.
> print(rownames(foo))
"a" "b" "c" "d"
and I try to access it via print(rownames(foo[bar]) it prints it as null.
E.g
> print(rownames(foo[2]))
NULL
Here in the second answer Richie Cotton explains this as "[...] that where there aren't any names, [...]"
This would indicate to me, that either rownames(foo) is empty - which is clearly not the case as I can print it with "print(rownames(foo))" - or that this method of access fails.
However when I try to change the value at position bar, i get a warning message, that the replacement length wouldn't match. However the operation nevertheless succeeds - which pretty much proves, that this method of access is indeed successful. E.g.
> bar = 2
> rownames(foo[bar]) = some.vector(rab)
> print(rownames(foo[bar])
NULL
> print(rownames(foo))
"a" "something else" "c" "d"
Why is this working? Obviously the function can't properly access the position of bar in foo, as it prints it as empty.
Why the heck does it still replace the value successfully and not fail in a horrific way?
Or asked the other way around: When it successfully replaces the value at this position why is the print function not returning the value properly?
LONG BACKGROUND EXPLANATION:
The data source contains the number in the list, the entrez-id of the gene, the official gene symbol, the affimetrix probe id and then the increase or decrease values. It looks something like this:
No Entrez Symbol Probe_id Sample1_FoldChange Sample2_FoldChange
1 690244 Sumo2 1367452_at 1.02 0.19
Later when displaying the data I want it to print out only the gene symbol and the increases.
Now if there is no gene-symbol in the data set it is printed as "n/a", this is obviously of no value for me, as I can't determine which one of many genes it is.
So I made a first processing step, that only for this cases exchanges the "n/a" result with "n/a(12345) where 12345 is the entrez-id.
I've written the following script to do this. (Note as I'm not a programmer and I am new with R I doubt that it is pretty code. But that's not the point I want to discuss.)
no.symbol.idx <-which(rownames(expr.table) == "n/a")
c1 <- character (length(rownames(expr.table)))
c2 <- c1
for (x in 1:length(c1))
{
c1[x] <- "n/a ("
}
for (x in 1:length(c2))
{
c2[x] <- ")"
}
rownames(expr.table)[no.symbol.idx] <- paste(c1, (expr.table[no.symbol.idx , "Entrez"]),c2, sep="")
The script works and it does what it should do. However I get the following error message.
Warning message:
In rownames(expr.table)[no.symbol.idx] <- paste(c1, (expr.table[no.symbol.idx, :
number of items to replace is not a multiple of replacement length
To find out what happened here is i put some text output into the script.
no.symbol.idx <-which(rownames(expr.table) == "n/a")
c1 <- character (length(rownames(expr.table)))
c2 <- c1
for (x in 1:length(c1))
{
c1[x] <- "n/a ("
}
for (x in 1:length(c2))
{
c2[x] <- ")"
}
print("print(rownames(expr.table)):")
print(rownames(expr.table))
print("print(no.symbol.idx):")
print(no.symbol.idx)
print("print(rownames(expr.table[no.symbol.idx])):")
print(rownames(expr.table[no.symbol.idx]))
print("print(rownames(expr.table[14])):")
print(rownames(expr.table[14]))
print("print(rownames(expr.table[15])):")
print(rownames(expr.table[15]))
cat("print(expr.table[no.symbol.idx,\"Entrez\"]):\n")
print(expr.table[no.symbol.idx,"Entrez"])
rownames(expr.table)[no.symbol.idx] <- paste(c1, (expr.table[no.symbol.idx , "Entrez"]),c2, sep="")
print("print(rownames(expr.table)):")
print(rownames(expr.table))
print("print(rownames(expr.table[no.symbol.idx])):")
print(rownames(expr.table[no.symbol.idx]))
And I get the following output in the console.
[1] "print(rownames(expr.table)):"
[1] "Sumo2" "Cdc37" "Copb2" "Vcp" "Ube2d3" "Becn1" "Lypla2" "Arf1" "Gdi2" "Copb1" "Capns1" "Phb2" "Puf60" "Dad1" "n/a"
[1] "print(no.symbol.idx):"
[1] 15
[1] "print(rownames(expr.table[no.symbol.idx])):"
NULL
[1] "print(rownames(expr.table[14])):"
NULL
[1] "print(rownames(expr.table[15])):"
NULL
... (to be continued)
so obviously no.symbol.idx gets the right position for the n/a value.
When I try to print it however it claims that rownames for this position was empty and returns NULL.
When I try to access this position "by hand" and use expr.table[15] it also returns NULL.
This however has nothing to do with the n/a value as the same holds true for the value stored at position 14.
... (the continuation)
print(expr.table[no.symbol.idx,"Entrez"]):
[1] "116727"
[1] "print(rownames(expr.table)):"
[1] "Sumo2" "Cdc37" "Copb2" "Vcp" "Ube2d3" "Becn1" "Lypla2" "Arf1" "Gdi2"
[10] "Copb1" "Capns1" "Phb2" "Puf60" "Dad1" "n/a (116727)"
[1] "print(rownames(expr.table[no.symbol.idx])):"
NULL
and this is the result that surprises me. Despite this it is working. It claims everything would be NULL but the operation is successful.
I don't understand this.
EDIT:
Here are the results of the functions you wanted me tu run.
str(expr.table)
chr [1:15, 1:17] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "401" "690244" "114562" "60384" ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:15] "Sumo2" "Cdc37" "Copb2" "Vcp" ...
..$ : chr [1:17] "No" "Entrez" "Symbol" "Probe_id" ...
head(expr.table)
dput(head(expr.table,10))
structure(c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"690244", "114562", "60384", "116643", "81920", "114558", "83510",
"64310", "29662", "114023", "Sumo2", "Cdc37", "Copb2", "Vcp",
"Ube2d3", "Becn1", "Lypla2", "Arf1", "Gdi2", "Copb1", "1367452_at",
"1367453_at", "1367454_at", "1367455_at", "1367456_at", "1367457_at",
"1367458_at", "1367459_at", "1367460_at", "1367461_at", "1.02000",
"-1.04000", "1.03000", "-0.12000", "-0.02000", "-0.03000", "0.09000",
"0.05000", "-0.09000", "0.16000", "0.19000", "0.11000", "-0.00425",
"0.52000", "0.46000", "0.42000", "0.20000", "0.05000", "0.21000",
"0.37000", "0.26000", "0.19000", "-0.03000", "0.35000", "0.34000",
"0.07000", "0.00156", "0.12000", "0.08000", "0.16000", "0.59000",
"0.20000", "-0.16000", "0.28000", "0.46000", "-0.15000", "0.00168",
"0.23000", "-0.01000", "0.10000", "0.05000", "0.12000", "-0.00522",
"0.58000", "0.23000", "0.06000", "0.01000", "0.07000", "-0.11000",
"0.23000", "-0.03", "0.08", "0.09", "0.08", "0.11", "0.03", "-0.08",
"0.02", "-0.05", "0.06", "0.03000", "-0.06000", "0.09000", "0.00940",
"0.11000", "-0.09000", "0.04000", "-0.04000", "-0.09000", "0.01000",
"0.04000", "-0.02000", "0.21000", "0.27000", "0.08000", "0.12000",
"0.06000", "0.26000", "0.04000", "0.40000", "0.05000", "0.05000",
"0.00897", "0.09000", "0.20000", "0.09000", "0.13000", "-0.03000",
"-0.08000", "-0.01000", "0.050000", "0.020000", "0.050000", "-0.005390",
"0.020000", "0.008080", "0.060000", "-0.030000", "-0.020000",
"-0.000406", "0.50", "0.11", "0.06", "0.19", "0.21", "0.32",
"0.15", "0.17", "0.14", "0.03", "-0.08000", "-0.11000", "-0.07000",
"0.03000", "-0.04000", "0.02000", "-0.00444", "-0.07000", "-0.13000",
"-0.11000", "0.25000", "0.15000", "0.22000", "0.74000", "0.39000",
"0.36000", "-0.08000", "0.18000", "0.00865", "0.43000"), .Dim = c(10L,
17L), .Dimnames = list(c("Sumo2", "Cdc37", "Copb2", "Vcp", "Ube2d3",
"Becn1", "Lypla2", "Arf1", "Gdi2", "Copb1"), c("No", "Entrez",
"Symbol", "Probe_id", "AA_HD_24h_FoldChange", "AAF_HD_24h_FoldChange",
"APAP_HD_24h_FoldChange", "BBZ_HD_24h_FoldChange", "BCT_HD_24h_FoldChange",
"BEA_HD_24h_FoldChange", "CBP_HD_24h_FoldChange", "CCL4_HD_24h_FoldChange",
"CPA_HD_24h_FoldChange", "CSP_HD_24h_FoldChange", "DEN_HD_24h_FoldChange",
"LS_HD_24h_FoldChange", "PCT_HD_24h_FoldChange")))
And here I added the file I use for debugging. This is the data it reads into expr.table.
No Entrez Symbol Probe_id AA_HD_24h_FoldChange AAF_HD_24h_FoldChange APAP_HD_24h_FoldChange BBZ_HD_24h_FoldChange BCT_HD_24h_FoldChange BEA_HD_24h_FoldChange CBP_HD_24h_FoldChange CCL4_HD_24h_FoldChange CPA_HD_24h_FoldChange CSP_HD_24h_FoldChange DEN_HD_24h_FoldChange LS_HD_24h_FoldChange PCT_HD_24h_FoldChange
1 690244 Sumo2 1367452_at 1.02 0.19 0.26 0.59 0.05 -0.03 0.03 0.04 0.05 0.05 0.5 -0.08 0.25
2 114562 Cdc37 1367453_at -1.04 0.11 0.19 0.2 0.12 0.08 -0.06 -0.02 0.05 0.02 0.11 -0.11 0.15
3 60384 Copb2 1367454_at 1.03 -4.25E-003 -0.03 -0.16 -5.22E-003 0.09 0.09 0.21 8.97E-003 0.05 0.06 -0.07 0.22
4 116643 Vcp 1367455_at -0.12 0.52 0.35 0.28 0.58 0.08 9.40E-003 0.27 0.09 -5.39E-003 0.19 0.03 0.74
5 81920 Ube2d3 1367456_at -0.02 0.46 0.34 0.46 0.23 0.11 0.11 0.08 0.2 0.02 0.21 -0.04 0.39
6 114558 Becn1 1367457_at -0.03 0.42 0.07 -0.15 0.06 0.03 -0.09 0.12 0.09 8.08E-003 0.32 0.02 0.36
7 83510 Lypla2 1367458_at 0.09 0.2 1.56E-003 1.68E-003 0.01 -0.08 0.04 0.06 0.13 0.06 0.15 -4.44E-003 -0.08
8 64310 Arf1 1367459_at 0.05 0.05 0.12 0.23 0.07 0.02 -0.04 0.26 -0.03 -0.03 0.17 -0.07 0.18
9 29662 Gdi2 1367460_at -0.09 0.21 0.08 -0.01 -0.11 -0.05 -0.09 0.04 -0.08 -0.02 0.14 -0.13 8.65E-003
10 114023 Copb1 1367461_at 0.16 0.37 0.16 0.1 0.23 0.06 0.01 0.4 -0.01 -4.06E-004 0.03 -0.11 0.43
11 29156 Capns1 1367462_at -0.23 0.32 0.11 0.13 -0.38 -0.15 -0.08 0.15 -0.18 0.2 0.13 -0.18 0.09
12 114766 Phb2 1367463_at 1.01E-003 0.29 0.41 0.59 0.05 -0.07 -0.13 -0.18 -0.28 -0.21 -0.22 -0.2 0.39
13 84401 Puf60 1367464_at -0.05 0.33 0.14 0.3 0.03 0.02 8.96E-003 2.96E-003 -8.63E-003 -0.13 0.07 -0.15 0.44
14 192275 Dad1 1367465_at 0.22 -0.21 -0.19 -0.24 -0.47 -0.01 -0.09 0.68 -0.06 -0.08 0.02 -0.29 -0.25
401 116727 n/a 1367852_s_at -0.34 -0.12 -0.06 -0.11 0.13 0.03 0.07 -0.18 0.08 -0.2 0.04 -0.04 0.06
Rownames is filled with the Gene symbols e.g Sumo2 for No 1.
What the script should do (and does) is for Entry No 401 it should change the name from n/a to n/a(116727). However the afforementioned warning occurs and I want to understand what's going on here.
I assume you are using a data.frame called foo. Underneath the hood, a data.frame is a list of vectors each of which is of the same length.
So foo[2] refers to the second column of foo as a dataframe, foo[,2] refers to the second column of foo as a vector. rownames(foo) is a vector and its second term is rownames(foo)[2]
If you want the second column of foo as a dataframe then you can use foo[2] or foo[,2,drop=FALSE] and print(rownames(foo[2])) will give you the same result as print(rownames(foo))
If you want the second row of foo as a dataframe then you need a comma as in foo[2,] and print(rownames(foo[2,])) will give you the same result as print(rownames(foo)[2])
If you want to change the name of the second row of foo in the original foo dataframe then try something like:
rownames(foo)[2] = "example of new name for row 2"

transpose 250,000 rows into columns in R

I always transpose by using t(file) command in R.
But i it is not running properly (not running at all) on big data file (250,000 rows and 200 columns). Any ideas.
I need to calculate correlation between 2nd row (PTBP1) with all other rows (except 8 rows including header). In order to do this I transpose rows to columns and then use cor function.
But I struck at transpose fn. Any help would be really appreciated!
I copied example from one of the post in stackoverflow (They are also almost discussing the same problem but seems no answer yet!)
ID A B C D E F G H I [200 columns]
Row0$-1 0.08 0.47 0.94 0.33 0.08 0.93 0.72 0.51 0.55
Row02$1 0.37 0.87 0.72 0.96 0.20 0.55 0.35 0.73 0.44
Row03$ 0.19 0.71 0.52 0.73 0.03 0.18 0.13 0.13 0.30
Row04$- 0.08 0.77 0.89 0.12 0.39 0.18 0.74 0.61 0.57
Row05$- 0.09 0.60 0.73 0.65 0.43 0.21 0.27 0.52 0.60
Row06-$ 0.60 0.54 0.70 0.56 0.49 0.94 0.23 0.80 0.63
Row07$- 0.02 0.33 0.05 0.90 0.48 0.47 0.51 0.36 0.26
Row08$_ 0.34 0.96 0.37 0.06 0.20 0.14 0.84 0.28 0.47
........
250,000 rows
Use a matrix instead. The only advantage of a dataframe over a matrix is the capacity to have different classes in the columns and you clearly do not have that situation, since a transposed dataframe could not support such a result.
I don't get why you want to transpose the data.frame. If you just use cor it doesn't matter if your data is in rows or columns.
Actually, it is one of the major advantages of R that it doen's matter if your data fits in the classical row-column pattern as SPSS and others programs require data to be.
There are numerous ways to correlate the first row with all other rows (I don't get which rows you want to exclude). One is using a loop (here the loop is implicit in the call to one of the *apply family functions):
lapply(2:(dim(fn)[1]), function(x) cor(fn[1,],fn[x,]))
Note that I expect you data.frame to ba called fn. To skip some rows change the 2 to the number you want. Furthermore, I would probably use vapply here.
I hope this answer points you in the correct direction and that is to not use t() if you absolutely don't need it.

Resources