2014-12-25 69 views
1

有一個問題,我試圖編寫腳本'kmeans.R',但我收到有關未找到變量的錯誤。該錯誤僅在我嘗試創建圖形但不返回數據框的函數中引發。下面是返回我需要用於隨後dataframes和圖形的參數的函數:編寫圖形函數時遇到變量範圍問題

simulations <- function(nrow, ncol){ 
    # Generate continuous normally distributed variables. 
    dataCont <- data.frame(matrix( 
    rnorm(nrow*ncol, mean=0,sd=1), nrow, ncol)) 

    # Generate categorical normally distributed variables. 
    dataCat <- data.frame(matrix(
    rnorm(nrow*ncol, mean=0,sd=1), nrow, ncol)) 
    dataCat[dataCat <= 0] <- 0 
    dataCat[dataCat > 0] <- 1 

    # return values 
    return(list(dataCont= dataCont, dataCat= dataCat)) 
} 

這是接受來自模擬參數的函數的例子()。

getKmeans <- function(dataCont, dataCat, dataOut){ 
    # Continuous variables 
    cont <- kmeans(dataCont, centers = 3, iter.max= 100, 
        algorithm="Hartigan-Wong") 

    # Categorical variables 
    cat <- kmeans(dataCat, centers = 3, iter.max= 100, 
        algorithm="Hartigan-Wong") 

    # Categorical as outliers 
    catout <- kmeans(dataOut, centers = 3, iter.max= 100, 
        algorithm="Hartigan-Wong") 
    return(list(cont= cont, 
       cat= cat, 
       catout= catout)) 
} 

以下是我如何從'kmeans.R'中調用getKmeans();它接受來自模擬()的參數沒有問題:

kmResults <- getKmeans(dataCont= sim$dataCont, 
         dataCat= sim$dataCat, 
         dataOut= dataOut) 

然而,當我使用下面的函數來創建一些散點圖:

scatterplots <- function(dataCont, dataCat, dataOut){ 
## Various scatterplots for generated data 

    ggplot(dataCont)+ 
    ggtitle("Continuous normally distributed variables") + 
    geom_point(aes(x=dataCont$X1, y=dataCont$X2), size=3) 
    ggsave("plots/cont_scatter.png", width=4, height=4, dpi=100) #plot 1 

    # scatterplot for categorical normally distributed vars. 
    ggplot(dataCat)+ 
    ggtitle("Categorical normally distributed variables") + 
    geom_point(aes(x=dataCat$X1, y=dataCat$X2), size=3) 
    ggsave("plots/cat_scatter.png", width=4, height=4, dpi=100) #plot 2 

# scatterplot for categorical outliers 
ggplot(dataOut)+ 
    ggtitle("Categorical variables with outliers") + 
    geom_point(aes(x=dataOut$X1, y=dataOut$X2), size=3) 
ggsave("plots/cat_scatter.png", width=4, height=4, dpi=100) #plot 3 
} 

則R引發錯誤指出「對象‘dataCont’未找到」。發生在我身上的唯一解釋是變量範圍確定存在問題。這不太合理,因爲只有當我調用生成一些圖的函數時纔會引發錯誤(使用ggplot2)。

您能否幫我理解解決此問題的最佳方法?謝謝你的時間。

回答

2

範圍問題與您如何在aes()中指定變量有關。與ggplot2一起使用時,您不符合some_data_frame$some_variable,只有some_variable。這應該可以解決問題:

scatterplots <- function(dataCont, dataCat, dataOut){ 
## Various scatterplots for generated data 

    ggplot(dataCont)+ 
    ggtitle("Continuous normally distributed variables") + 
    geom_point(aes(x=X1, y=X2), size=3) 
    ggsave("plots/cont_scatter.png", width=4, height=4, dpi=100) #plot 1 

    # scatterplot for categorical normally distributed vars. 
    ggplot(dataCat)+ 
    ggtitle("Categorical normally distributed variables") + 
    geom_point(aes(x=X1, y=X2), size=3) 
    ggsave("plots/cat_scatter.png", width=4, height=4, dpi=100) #plot 2 

# scatterplot for categorical outliers 
ggplot(dataOut)+ 
    ggtitle("Categorical variables with outliers") + 
    geom_point(aes(x=X1, y=X2), size=3) 
ggsave("plots/cat_scatter.png", width=4, height=4, dpi=100) #plot 3 
}