Vimal
Vimal

Reputation: 1

How to store values from loop to a dataframe in R?

I am new to R and programming, I want to store values from loop to a data frame in R. I want ker, cValues, accuracyValues values to be stored a data frame from bellow code. I am not able to achieve this, Data Frame is only saving last value not all the values.

Can you please help me with this please.

# Define a vector which has different kernel methods
kerna <- c("rbfdot","polydot","vanilladot","tanhdot","laplacedot",
           "besseldot","anovadot","splinedot")

# Define a for loop to calculate accuracy for different values of C and kernel
for (ker in kerna){
    cValues <- c()
    accuracyValues <- c()
    for (c in 1:100) {
        model <- ksvm(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,
                      data = credit_card_data,
                      type ="C-svc",
                      kernel = ker,
                      C=c,
                      scaled =TRUE)
        pred <- predict(model,credit_card_data[,1:10])
        #pred
        accuracy <- sum(pred== credit_card_data$V11)/nrow(credit_card_data)

        cValues[c] <- c;
        accuracyValues[c] <- accuracy;

    }

    for(i in 1:100) {
        print(paste("kernal:",ker, "c=",cValues[i],"accuracy=",accuracyValues[i]))


    }

}

Upvotes: 0

Views: 1090

Answers (2)

Parfait
Parfait

Reputation: 107632

Consider Map to build a list of data frames from each pairing of ker and cValues (1:100) generated from expand.grid and row bind all elements together.

k_c_pairs_df <- expand.grid(kerna=kerna, c_value=1:100, stringsAsFactors = FALSE)

model_fct <- function(ker, c) {
      model <- ksvm(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,
                    data = credit_card_data,
                    type ="C-svc",
                    kernel = ker,
                    C=c,
                    scaled =TRUE)

      pred <- predict(model,credit_card_data[,1:10])
      accuracy <- sum(pred== credit_card_data$V11)/nrow(credit_card_data)

      print(paste("kernal:",ker, "c=",cValues[i],"accuracy=",accuracyValues[i]))

      return(data.frame(kernel = ker, cValues = c, accuracyValues = accuracy))
}

df_list <- Map(model_fct, k_c_pairs_df$ker, k_c_pairs_df$c_value)

final_df <- do.call(rbind, df_list)

Upvotes: 0

Ben Fasoli
Ben Fasoli

Reputation: 526

Starting from your base code, set up the structure of the output data frame. Then, loop through and fill in the accuracy values on each iteration. This method also "flattens" the nested loop and gets rid of your c variable which conflicts with the built-in c() function.

kerna <- c("rbfdot","polydot","vanilladot","tanhdot","laplacedot",
           "besseldot","anovadot","splinedot")

# Create dataframe to store output data
df <- data.frame(kerna = rep(kerna, each = 100), 
                 cValues = rep(1:100, times = length(kerna)),
                 accuracyValues = NA,
                 stringsAsFactors = F)

# Define a for loop to calculate accuracy for different values of C and kernel
for (i in 1:nrow(df)){
  ker <- df$kerna[i]
  j <- df$cValues[i]
  model <- ksvm(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,
                data = credit_card_data,
                type ="C-svc",
                kernel = ker,
                C=j,
                scaled =TRUE)
  pred <- predict(model,credit_card_data[,1:10])
  accuracy <- sum(pred== credit_card_data$V11)/nrow(credit_card_data)

  # Insert accuracy into df$accuracyValues
  df$accuracyValues[i] <- accuracy;
}

Upvotes: 1

Related Questions