我有三个文本文件。我想做一些如下所示的计算并写出结果。所有文本文件包含14列X1到X14和601行。此代码基本上计算所有三个文件之间的相关性(例如,来自ref文件的x1和来自文件sour1的相同列x1,依此类推),并进行一些计算,然后返回结果。
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
}
)
编写结果时出错:
write.table(result1,file = "foo.text")
Error in is.data.frame(x) : object 'result1' not found
我的档案样本:
"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")
答案 0 :(得分:2)
您收到该错误是因为函数内部创建的变量超出了该函数范围之外的范围,即当评估跟随函数时,R
不知道result1
和{{1}的存储值在该函数之外使用
result2
要演示我刚才所说的内容,请尝试下面的代码
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
}
您收到如下错误:
testFunc <- function() {
a <- 0
}
testFunc()
print(a)
因此,正如Error in print(a) : object 'a' not found
所建议的那样,您必须分别为SimonO101
和lapply
分配sour1
次调用的结果。
sour2
答案 1 :(得分:1)
您需要将result1分配给lapply.i.e。
result1 <- lapply( columns , function(column) { cor(ref[[column]],sour2[[column]],use = "na.or.complete") } )
为结果2做同样的事情:
result2 <- lapply( columns , function( column ) { cor(ref[[column]],sour1[[column]],use = "na.or.complete") } )
要分离结果,请尝试更改代码以格式化您的结果......
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 )