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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:06:58+00:00 2026-06-08T17:06:58+00:00

I am attempting to reformat the data set my.data to obtain the output shown

  • 0

I am attempting to reformat the data set my.data to obtain the output shown below the my.data2 statement. Specifically, I want to put the last 4 columns of my.data on one line per record.id, where the last four
columns of my.data will occupy columns 2-5 of the new data matrix if group=1 and columns 6-9 if group=2.

I wrote the cumbersome code below, but the double for-loop is causing an error that I simply cannot locate.
Even if the double for-loop worked, I suspect there is a much more efficient way of accomplishing the
same thing – (maybe reshape?)

Thank you for any help correcting the double for-loop or with more efficient code.

my.data <-  "record.id group s1 s2 s3 s4
    1  1      2      0      1      3
    1  2      0      0      0     12
    2  1      0      0      0      0
    3  1     10      0      0      0
    4  1      1      0      0      0
    4  2      0      0      0      0
    8  2      0      2      2      0
    9  1      0      0      0      0
    9  2      0      0      0      0"    

my.data2 <- read.table(textConnection(my.data), header=T)

# desired output
#
# 1     2      0      1      3      0      0      0     12
# 2     0      0      0      0      0      0      0      0
# 3    10      0      0      0      0      0      0      0
# 4     1      0      0      0      0      0      0      0
# 8     0      0      0      0      0      2      2      0
# 9     0      0      0      0      0      0      0      0

Code:

dat_sorted <- sort(unique(my.data2[,1]))
my.seq <- match(my.data2[,1],dat_sorted)

my.data3 <- cbind(my.seq, my.data2)

group.min <- tapply(my.data3$group, my.data3$my.seq, min)
group.max <- tapply(my.data3$group, my.data3$my.seq, max)

# my.min <- group.min[my.data3[,1]]
# my.max <- group.max[my.data3[,1]]

my.records <- matrix(0, nrow=length(unique(my.data3$record.id)), ncol=9)

x <- 1

for(i in 1:max(my.data3$my.seq)) {

   for(j in group.min[i]:group.max[i]) {

      if(my.data3[x,1] == i) my.records[i,1]   = i

      # the two lines below seem to be causing an error
      if((my.data3[x,1] == i) & (my.data3[x,3] == 1)) (my.records[i,2:5] = my.data3[x,4:7])
      if((my.data3[x,1] == i) & (my.data3[x,3] == 2)) (my.records[i,6:9] = my.data3[x,4:7])

      x <- x + 1

   }
}
  • 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-08T17:07:00+00:00Added an answer on June 8, 2026 at 5:07 pm

    You are right, reshape helps here.

    library(reshape2)
    m <- melt(my.data2, id.var = c("record.id", "group"))
    dcast(m, record.id ~ group + variable, fill = 0)
      record.id 1_s1 1_s2 1_s3 1_s4 2_s1 2_s2 2_s3 2_s4
    1         1    2    0    1    3    0    0    0   12
    2         2    0    0    0    0    0    0    0    0
    3         3   10    0    0    0    0    0    0    0
    4         4    1    0    0    0    0    0    0    0
    5         8    0    0    0    0    0    2    2    0
    6         9    0    0    0    0    0    0    0    0
    

    Comparison:

    dfTest <- data.frame(record.id = rep(1:10e5, each = 2), group = 1:2, 
    s1 = sample(1:10, 10e5 * 2, replace = TRUE), 
    s2 = sample(1:10, 10e5 * 2, replace = TRUE), 
    s3 = sample(1:10, 10e5 * 2, replace = TRUE), 
    s4 = sample(1:10, 10e5 * 2, replace = TRUE))
    
    
    system.time({
    ...# Your code
    })
    Error in my.records[i, 1] = i : incorrect number of subscripts on matrix
    Timing stopped at: 41.61 0.36 42.56 
    
    system.time({m <- melt(dfTest, id.var = c("record.id", "group"))
                  dcast(m, record.id ~ group + variable, fill = 0)})
       user  system elapsed 
      25.04    2.78   28.72
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to use the amazon API to obtain product data and currently failing miserably.
I have two large data sets and I am attempting to reformat the older
Attempting to set up an automated texting service for customers, where people can text
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to set up Samba + OpenLDAP using nss_ldap. After joining Windows7 to Samba
Attempting to deserialize JSON data and update each object's prototype and inherit a common
Attempting to set the tabIndex for date_select has proven to be difficult out of
Attempting something like public string MyText { get {return lbl.Text;} set {lbl.Text = value;}}
attempting a first Blackberry App. It will display diary data (eventually). I'm just trying
Attempting to take the data in my gridview and convert it back into a

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.