用R中的引用值替换字符串向量中的变量

时间:2015-02-25 19:25:45

标签: r

我正在尝试写这个for循环:

    for (j in c("mxPH", "mnO2", "Cl", "NO3", "NH4", "oPO4", "PO4", "Chla", "a1", "a2", "a3", "a4", "a5", "a6", \
"a7"))
{
    for (i in c("summer", "autumn", "winter", "spring"))
        {
            maximum <- max(algae[algae$j>0 & algae$season == i,][, j], na.rm=T)
            message("The maximum of ", j, " for ", i," is: ", maximum)

            median <- median(algae[algae$j>0 & algae$season == i,][, j], na.rm=T)
            message("The median of ", j, " for ",i," is: ", median)

            mean <- mean(algae[algae$j>0 & algae$season == i,][, j], na.rm=T)
            message("The mean of ", j, " for ",i," is: ", mean)
        }
}

我认为问题在于变量j被其值替换而没有任何(最大,中值和平均)行所需的引号,我试图用这个版本来解决:

    for (j in c("mxPH", "mnO2", "Cl", "NO3", "NH4", "oPO4", "PO4", "Chla", "a1", "a2", "a3", "a4", "a5", "a6", \
"a7"))
{
    for (i in c("summer", "autumn", "winter", "spring"))
        {
            mito <- paste("",j,"", sep="")
            maximum <- max(algae[algae$j>0 & algae$season == i,][, mito], na.rm=T)
            message("The maximum of ", j, " for ", i," is: ", maximum)

            median <- median(algae[algae$j>0 & algae$season == i,][, mito], na.rm=T)
            message("The median of ", j, " for ",i," is: ", median)

            mean <- mean(algae[algae$j>0 & algae$season == i,][, mito], na.rm=T)
            message("The mean of ", j, " for ",i," is: ", mean)
        }
}

哪个不起作用。我怎么能正确地写这个for循环?

提前谢谢大家

编辑: Here是我用于数据的文件:

2 个答案:

答案 0 :(得分:2)

简化了一点:

molecule<-c("mxPH", "mnO2", "Cl", "NO3", "NH4", "oPO4", "PO4", "Chla", "a1", "a2", "a3", "a4", "a5", "a6","a7")
season<-c("summer", "autumn", "winter", "spring")

for (j in 1:length(molecule))
 {
  for (i in 1:length(season))
    {

        seasoni<-season[i]
        moleculej<-molecule[j]
        dat<-as.numeric(unlist(subset(algae,algae[,moleculej]>0 & algae$season == seasoni,select=moleculej)))
        maximum <- max(dat, na.rm=T)
        print(paste0("The maximum of ", moleculej, " for ", seasoni," is: ", maximum))

        med <- median(dat, na.rm=T)
        print(paste0("The median of ", moleculej, " for ",seasoni," is: ", med))

        meane <- mean(dat, na.rm=T)
        print(paste0("The mean of ", moleculej, " for ",seasoni," is: ", meane))
    }
  }

输出段:

[1] "The maximum of mxPH for summer is: 8.15"
[1] "The median of mxPH for summer is: 7.585"
[1] "The mean of mxPH for summer is: 7.6675"
[1] "The maximum of mxPH for autumn is: 8.1"
[1] "The median of mxPH for autumn is: 8.055"
[1] "The mean of mxPH for autumn is: 7.94"
[1] "The maximum of mxPH for winter is: 8.7"
[1] "The median of mxPH for winter is: 7.93"
[1] "The mean of mxPH for winter is: 8.04285714285714"
[1] "The maximum of mxPH for spring is: 8.35"
[1] "The median of mxPH for spring is: 7.79"
[1] "The mean of mxPH for spring is: 7.904"

答案 1 :(得分:1)

如果您想将字符串组合在一起制作信息,请使用粘贴功能。

所以而不是

message("The mean of ", j, " for ",i," is: ", mean)

message(paste("The mean of ", j, " for ",i," is: ", mean, sep=""))