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

  • Home
  • SEARCH
  • 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 9046217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:36:16+00:00 2026-06-16T11:36:16+00:00

I need to transform following (simplified) dataset, created by following code: structure(list(W1.1 = structure(c(1L,

  • 0

I need to transform following (simplified) dataset, created by following code:

structure(list(W1.1 = structure(c(1L, NA, NA), .Names = c("case1", 
"case2", "case3"), .Label = "1", class = "factor"), R1.1 = structure(c(1L, 
NA, NA), .Names = c("case1", "case2", "case3"), .Label = "2", class = "factor"), 
    W1.2 = structure(c(NA, 1L, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R1.2 = structure(c(NA, 
    1L, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W2.1 = structure(c(NA, 1L, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R2.1 = structure(c(NA, 
    1L, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W2.2 = structure(c(1L, NA, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "2", class = "factor"), R2.2 = structure(c(1L, 
    NA, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W3.1 = structure(c(1L, NA, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R3.1 = structure(c(1L, 
    NA, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    W3.2 = structure(c(1L, 1L, NA), .Names = c("case1", "case2", 
    "case3"), .Label = "1", class = "factor"), R3.2 = structure(c(1L, 
    1L, NA), .Names = c("case1", "case2", "case3"), .Label = "1", class = "factor"), 
    age = structure(c(3L, 1L, 2L), .Names = c("case1", "case2", 
    "case3"), .Label = c("20", "48", "56"), class = "factor"), 
    gender = structure(c(2L, 1L, 2L), .Names = c("case1", "case2", 
    "case3"), .Label = c("female", "male"), class = "factor")), .Names = c("W1.1", 
"R1.1", "W1.2", "R1.2", "W2.1", "R2.1", "W2.2", "R2.2", "W3.1", 
"R3.1", "W3.2", "R3.2", "age", "gender"), row.names = c(NA, 3L
), class = "data.frame")

For the new data I want:
– a row dedicated to every x.x, with info on the Rx.x value, age and gender.
– only have a row returned when Wx.x was 1. When 2 or NA, I don’t need it.

For my example this dataset should look something like this:

  incident type Where Reported age gender
1        1  1.1     1        2  56   male
2        2  3.1     1        1  56   male
3        3  3.2     1        1  56   male
4        4  1.2     1        1  20 female
5        5  2.1     1        1  20 female
6        6  3.2     1        1  20 female

Note: the “Where” column can even be omitted since it should be a constant vector of 1, and I don’t need it for the analysis.

  • 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-16T11:36:16+00:00Added an answer on June 16, 2026 at 11:36 am

    This is (mostly) a problem to be tackled by reshape(). Assuming your original dataset is called “temp”:

    First, reshape it from a wide format to a long format.

    temp.long <- reshape(temp, direction = "long",
                         idvar=c("age", "gender"), 
                         varying = which(!names(temp) %in% c("age", "gender")), 
                         sep = "")
    temp.long
    #               age gender time    W    R
    # 56.male.1.1    56   male  1.1    1    2
    # 20.female.1.1  20 female  1.1 <NA> <NA>
    # 48.male.1.1    48   male  1.1 <NA> <NA>
    # 56.male.1.2    56   male  1.2 <NA> <NA>
    # 20.female.1.2  20 female  1.2    1    1
    # 48.male.1.2    48   male  1.2 <NA> <NA>
    # 56.male.2.1    56   male  2.1 <NA> <NA>
    # 20.female.2.1  20 female  2.1    1    1
    # 48.male.2.1    48   male  2.1 <NA> <NA>
    # 56.male.2.2    56   male  2.2    2    1
    # 20.female.2.2  20 female  2.2 <NA> <NA>
    # 48.male.2.2    48   male  2.2 <NA> <NA>
    # 56.male.3.1    56   male  3.1    1    1
    # 20.female.3.1  20 female  3.1 <NA> <NA>
    # 48.male.3.1    48   male  3.1 <NA> <NA>
    # 56.male.3.2    56   male  3.2    1    1
    # 20.female.3.2  20 female  3.2    1    1
    # 48.male.3.2    48   male  3.2 <NA> <NA>
    

    Second, do some cleanup.

    temp.long <- na.omit(temp.long)
    temp.long <- temp.long[-which(temp.long$W == 2), ]
    temp.long <- temp.long[order(rev(temp.long$gender), temp.long$time), ]
    rownames(temp.long) <- NULL
    temp.long$incident <- seq(nrow(temp.long))
    temp.long
    #   age gender time W R incident
    # 1  56   male  1.1 1 2        1
    # 2  56   male  3.1 1 1        2
    # 3  56   male  3.2 1 1        3
    # 4  20 female  1.2 1 1        4
    # 5  20 female  2.1 1 1        5
    # 6  20 female  3.2 1 1        6
    

    You can do further cleanup to change your column names and column order if it’s important.

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

Sidebar

Related Questions

I need to transform the following in VB.NET from C# (.NET 4) class XXX:
I have a following XML structure that I need to transform: <recordset rowCount=68 fieldNames=ITEM,ECL,LEAD_TIME
I need to maintain/install the following java code: javax.xml.transform.Transformer t = tf.newTransformer(new javax.xml.transform.stream.StreamSource(foo.xsl)) ;
I need to transform my nested sets structure (mysql) into json for this spacetree
I need to transform a list into dictionary as follows. The odd elements has
I need to transform the following xml doc: <a> <b/> <c/> myText </a> into
Lets say i have the following web page: <html> <body> <div class="transform"> <span>1</span> </div>
I am using the following code to transform a universal time code into something
I want to transform the following c code to TCL. int a[10],b[10]; int n=20
Suppose I have the following xml, what xsl do I need to transform 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.