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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:05:24+00:00 2026-05-29T07:05:24+00:00

I have a dataset that looks like the one below, and I would like

  • 0

I have a dataset that looks like the one below, and I would like to create a new variable based on these variables, which can be used with the other variables in the dataset.

The first variable, ID, is a respondent identification number. The med variable are 1 and 2, indicating different treatments. Var1_v1 and Var1_v2 has four real options 1,2,3, or 9, and these options are only given to those who med ==1. If med ==2, NA appears in the Var1s. Var2 receives NA when med ==1 and has real values ranging from 1-3 when med==2.

ID <- c(1,2,3,4,5,6,7,8,9,10,11)
med <- c(1,1,1,1,1,1,2,2,2,2,2)
Var1_v1 <- c(2,2,3,9,9,9,NA,NA,NA,NA,NA) #ranges from 1-3, and 9
Var1_v2 <- c(9,9,9,1,3,2,NA,NA,NA,NA,NA) #ranges from 1-3, and 9
Var2 <- c(NA,NA,NA,NA,NA,NA,3,3,1,3,2)

#tables to show you what data looks like relative to med var
table(Var1_v1, med)
table(Var1_v2, med)
table(Var2, med)

I’ve been looking around for a while to figure out a recoding/new variable creation code, but I have had no luck.

Ultimately, I would like to create a new variable, say Var3, based on three conditions:

  • Uses the values from Var1_v1 if the value = 1, 2, or 3
  • Uses the values from Var1_v2 if the value = 1, 2, or 3
  • uses the values from Var2 if the values = 1, 2, or 3

And this variable should be able to match up with the ID number, so that it can be used within the dataset.

So the final variable should look like:

 Var3 <- (2,2,3,1,3,2,3,3,1,3,2)

Thanks!

  • 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-05-29T07:05:25+00:00Added an answer on May 29, 2026 at 7:05 am

    Something like

    v <- Var1_v1
    v[Var1_v2 %in% 1:3] <- Var1_v2[Var1_v2 %in% 1:3]
    v[Var2 %in% 1:3] <- Var2[Var2 %in% 1:3]
    v
    [1] 2 2 3 1 3 2 3 3 1 3 2
    

    which uses one of them as a base (you could also use a pure NA vector) and simply fills in only parts that match.

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

Sidebar

Related Questions

SQL 2008 Basically i have a dataset that looks like this: AcctID AcctType AcctSubType
I have a dataset with three variables. One continous independent variable, one continous dependent
I have a dataset that looks like this: 1 -0.151714363660730E+03 0.681572558518519E+02 -0.147787110884357E+03 0.702453634941157E+02 -0.147765104000000E+03
I have a data set that looks like: Files Batch filepath1.txt One filepath2.txt One
I have a dataset that looks something like this: IDnum State Product Consumption 123
I have a (dense) dataset that consist of 5 groups, so my data.frame looks
I have a UltraWinGrid that is bound to a DataSet, in which a couple
I've got a bunch of strongly typed DataSet that each have two tables. One
I have a SqlQuery that looks like this: SqlQuery query = DB.Select( Order.Schema.TableName +
I am working with a dataset in SAS, containing many variables. One of these

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.