Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:17:43+00:00 2026-06-05T19:17:43+00:00

I was able to successfully run an RF model using some R code I

  • 0

I was able to successfully run an RF model using some R code I was given. That is below and it includes snippet of my data too.

The only problem is that the way the code is written it only outputs a vector of probabilities and no data from the original test data set called “testset”. So now I am trying to figure out how to output my probabilities along with the original data frame because I couldn’t find a solution online. In other words I want it to be another column in the data set, like right after my FLSAStat column. That’s so I can then output all of ittogether to a csv file.

Here’s what I have:

#####################################################
# 1. SETUP DATA
#####################################################
mydata <- read.csv("train_test.csv", header=TRUE)
colnames(testset)
[1] "train"           "Target"          "ApptCode"        "Directorate"         "New_Discipline"  "Series"          "Adjusted.Age"   
[8] "Adj.Service"     "Adj.Age.Service" "HiEducLv"        "Gender"           "RetCd"           "FLSAStat"  
> head(testset)
 train Target ApptCode                Directorate             New_Discipline Series  Adjusted.Age Adj.Service Adj.Age.Service HiEducLv Gender
5909     0     NA       IN                   Business Math  Computer Science  IT     PSTS        54.44          10           64.44 Bachelor   Male
5910     0     NA       IN                Computation Math  Computer Science  IT   PSTS        51.51          15           66.51 Bachelor   Male
5911     0     NA       IN Physical and Life Sciences                    Physics   PSTS        40.45           5           45.45      PHD   Male
5912     0     NA       IN  Weapons and Complex Integ                    Physics   PSTS        62.21          35           97.21      PHD   Male
5913     0     NA       IN  Weapons and Complex Integ                    Physics   PSTS        45.65          15           60.65      PHD   Male
5914     0     NA       FX Physical and Life Sciences                    Physics   PSTS        36.13           5           41.12      PHD   Male
  RetCd FLSAStat
5909  TCP2        E
5910  TCP2        E
5911  TCP2        E
5912  TCP2        E
5913  TCP1        E
5914  TCP2        E    

#create train and test sets
trainset = mydata[mydata$train == 1,]
testset = mydata[mydata$train == 0,]
#eliminate unwanted columns from train set 
trainset$train = NULL
#####################################################
# 2. set the formula
#####################################################
theTarget <- "Target"
theFormula <- as.formula(paste("as.factor(",theTarget, ") ~ . "))
theFormula1 <- as.formula(paste(theTarget," ~ . "))
trainTarget = trainset[,which(names(trainset)==theTarget)]
testTarget  = testset[,which(names(testset)==theTarget)]

#####################################################
# Random Forest
#####################################################
library(randomForest)
what <- "Random Forest"
FOREST_model <- randomForest(theFormula, data=trainset, ntree=500)
train_pred <- predict(FOREST_model, trainset, type="prob")[,2]
test_pred <- predict(FOREST_model, testset, type="prob")[,2]
display_results()
testID  <- testset$case_id
predictions <- test_pred
submit_file = cbind(testID,predictions)
write.csv(submit_file, file="RANDOM4.csv", row.names = FALSE)

I think the problem is that I am lacking an additional line of code that does the merging of the predictions vector back into testSet. I’m guessing this this would go somewhere before the third to last line of code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:17:46+00:00Added an answer on June 5, 2026 at 7:17 pm

    Just add the column to your dataframe like so:

    testset$Predictions <- test_pred
    write.csv(testset, file="RANDOM4.csv", row.names = FALSE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following code, I'm able to successfully open a raw disk on my
Has any been able to successfully run with a client using the WSO2/C++ web
I am successfully able to authenticate Facebook and Google accounts using my Oauth2 servlets.
So I was successfully able to run a command line application with various arguments
I have a php script that needs to run for quite some time. What
I was able to successfully run a simple test for RavenDB based on the
I'm working on an app and I've been able to successfully debug it using
I was able to successfully run suites of tests before upgrading to Karmic. Now
I have an asterisk-based PBX, and I have been able to successfully run an
Using Backbone.JS, I am able to successfully create new models and save them to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.