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 8615599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:25:04+00:00 2026-06-12T05:25:04+00:00

I am scoring a psychometric instrument at work and want to recode a few

  • 0

I am scoring a psychometric instrument at work and want to recode a few variables. Basically, each question has five possible responses, worth 0 to 4 respectively. That is how they were coded into our database, so I don’t need to do anything except sum those. However, there are three questions that have reversed scores (so, when someone answers 0, we score that as 4). Thus, I am “reversing” those ones.

The data frame basically looks like this:

studyid  timepoint      date      inst_q01  inst_q02  ...  inst_q20
   1         2       1995-03-13       0         2     ...      4
   2         2       1995-06-15       1         3     ...      4

Here’s what I’ve done so far.

# Survey Processing
# Find missing values (-9) and confusions (-1), and sum them
project_f03$inst_nmiss <- rowSums(project_f03[,4:23]==-9)
project_f03$inst_nconfuse <- rowSums(project_f03[,4:23]==-1)
project_f03$inst_nmisstot <- project_f03$inst_nmiss + project_f03$inst_nconfuse

# Recode any missing values into NAs
for(x in 4:23) {project_f03[project_f03[,x]==-9 | project_f03[,x]==-1,x] <- NA}
rm(x)

Now, everything so far is pretty fine, I am about to recode the three reversed ones. Now, my initial thought was to do a simple loop through the three variables, and do a series of assignment statements something like below:

# Questions 3, 11, and 16 are reversed
for(x in c(3,11,16)+3) {

    project_f03[project_f03[,x]==4,x] <- 5
    project_f03[project_f03[,x]==3,x] <- 6
    project_f03[project_f03[,x]==2,x] <- 7
    project_f03[project_f03[,x]==1,x] <- 8
    project_f03[project_f03[,x]==0,x] <- 9
    project_f03[,x] <- project_f03[,x]-5
}
rm(x)

So, the five assignment statements just reassign new values, and the loop just takes it through all three of the variables in question. Since I was reversing the scale, I thought it was easiest to offset everything by 5 and then just subtract five after all recodes were done. The main issue, though, is that there are NAs and those NAs result in errors in the loop (naturally, NA==4 returns an NA in R). Duh – forgot a basic rule!

I’ve come up with three alternatives, but I’m not sure which is the best.

  • First, I could obviously just move the NA-creating code after the loop, and it should work fine. Pros: easiest to implement. Cons: Only works if I am receiving data with no innate (versus created) NAs.
  • Second, I could change the logic statement to be something like:
    project_f03[!is.na(project_f03[,x]) && project_f03[,x]==4,x] which should eliminate the logic conflict. Pros: not too hard, I know it works. Cons: A lot of extra code, seems like a kludge.
  • Finally, I could change the logic from
    project_f03[project_f03[,x]==4,x] <- 5 to
    project_f03[project_f03[,x] %in% 4,x] <- 5. This seems to work fine, but I’m not sure if it’s a good practice, and wanted to get thoughts. Pros: quick fix for this issue and seems to work; preserves general syntatic flow of “blah blah LOGIC blah <- bleh”. Cons: Might create black hole? Not sure what the potential implications of using %in% like this might be.

EDITED TO MAKE CLEAR

This question has one primary component: Is it safe to utilize %in% as described in the third point above when doing logical operations, or are there reasons not to do so?

The second component is: What are recommended ways of reversing the values, like some have described in answers and comments?

  • 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-12T05:25:05+00:00Added an answer on June 12, 2026 at 5:25 am

    The straightforward answer is that there is no black hole to using %in%. But in instances where I want to just discard the NA values, I’d use which: project_f03[which(project_f03[,x]==4),x] <- 5

    %in% could shorten that earlier bit of code you had:

    for(x in 4:23) {project_f03[project_f03[,x]==-9 | project_f03[,x]==-1,x] <- NA}
    #could be
    for(x in 4:23) {project_f03[project_f03[,x] %in% c(-9,-1), x] <- NA}
    

    Like @flodel suggested, you can replace that whole block of code in your for-loop with project_f03[,x] <- rev(0:4)[match(project_f03[,x], 0:4, nomatch=10)]. It should preserve NA. And there are probably more opportunities to simplify code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Scoring System In Cocos2D I got a reply from a question I
I have a directory with 260+ text files containing scoring information. I want to
Let's say I'm making an Use Case for a game that has a scoring
I am having difficulty getting this scoring function to work. The objective of my
I have an app which has scoring and other information stored in a database.
I'm designing a program to solve solitaire games in the highest-scoring way possible. The
I want to promote users to moderators, admins, etc. What about scoring and game
I have a Vote-scoring system, each user can score any product each day (maximum
I want to return results to a user sorted by a scoring algorithm that
Has someone successfully overridden the scoring of documents in a query so that the

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.