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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:17:12+00:00 2026-06-17T15:17:12+00:00

I have a following stata code which I am trying to convert to R

  • 0

I have a following stata code which I am trying to convert to R:

dataframe

    y1  y2  y3  y4  y5  y6  y11 y12 y13 y14 y15 y16
    5   0   0   0   0   0   0   0   0   0   0   0
    5   0   0   0   0   0   0   0   0   0   0   0
    5   0   0   0   0   0   0   0   0   0   0   0
    5   0   0   0   0   0   0   0   0   0   0   0
    5   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   1   2   1   2   0   0
    0   0   0   0   0   0   1   1   1   2   0   0
    0   0   0   0   0   0   1   8   1   2   0   0
    0   0   0   0   0   0   1   1   1   2   0   0
    0   0   0   0   0   0   1   1   1   2   0   0
    1   1   0   0   0   0   0   0   0   0   0   0
    1   1   0   0   0   0   0   0   0   0   0   0
    1   1   0   0   0   0   0   0   0   0   0   0
    1   1   0   0   0   0   0   0   0   0   0   0
    2   2   5   1   1   2   2   2   1   1   2       1

local z1 "y1 y12 y3 y4 y5 y6"
local z2 "y11 y12 y13 y14 y15 y16"
local i = 1
local n : word count `z1'
gen k=.

while `i'<=`n' {

    local z1`i' : word `i' of `z1'
        local z2`i' : word `i' of `z2'
        replace k=max(0,`z1`i'')*(`z2`i''==2|`z2`i''==1)
        local i=`i'+1
    } 

Expected output:

k
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2

I used the following equivalent R code:

      dataframe$z1<- "y1 y12 y3 y4 y5 y6"
      dataframe$z2<- "y11 y12 y13 y14 y15 y16"
      i<-  1
      n<-sapply(gregexpr("\\W+", z1), length) + 1
      dataframe$k<-NA

    for (j in i:n){
  .... #I wanted to refer to each word of z1 
  ...#e.g.,dataframe$z1[i]<-which gives word i of z1 
  .. #I wanted to refer to each word of z2
  ... #e.g.,dataframe$z1[i]<-whicg gives word i of z2 

   dataframe$k<-with(dataframe, pmax(0,z1[j])*ifelse(z2[j] %in% c(1,2),1,0))

}

The dotted lines indicate that I was not able to find the equivalent code in R. I would appreciate if you could help me in this regard.

    # Updated Stata codes and data (number of observation is reduced to 10)

y1  y2  y3  y4  y5  y6  y11 y12 y13 y14 y15 y16
5   0   0   0   0   0   0   0   0   0   0   0
5   0   0   0   0   0   0   0   0   0   0   0
5   0   0   0   0   0   0   0   0   0   0   0
5   0   0   0   0   0   0   0   0   0   0   0
5   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0

y111    y112    y113    y114    y115    y116    y1111   y1112   y1113   y1114   y1115   y1116
1   0   0   0   0   0   81000   0   0   0   0   0
1   0   0   0   0   0   86000   0   0   0   0   0
1   0   0   0   0   0   96000   0   0   0   0   0
1   0   0   0   0   0   84000   0   0   0   0   0
1   0   0   0   0   0   76000   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0

    local z1 "y1 y2 y3 y4 y5 y6"
    local z2 "y11 y12 y13 y14 y15 y16"
    local z3 "y111 y112 y113 y114 y115 y116"
    local z4 "y1111 y1112 y1113 y1114 y1115 y1116"
    local i = 1
    local n : word count `z1'
    gen k=.
    gen r=0
    gen s=0
    gen t=0
    while `i'<=`n' {

        local z1`i' : word `i' of `z1'
            local z2`i' : word `i' of `z2'
            local z3`i' : word `i' of `z3'
            local z4`i' : word `i' of `z4'


            replace k=max(0,`z4`i'')*(`z1`i''==5|`z1`i''==10|`z2`i''==2|`z2`i''==1|`z3`i''==1)
            replace r=r+k if `i'<=3
            replace s=s+k if `i'>3
            replace t=t+k
            local i=`i'+1
        } 

#Expected output

t       r   s       k
81000   81000   0   0
86000   86000   0   0
96000   96000   0   0
84000   84000   0   0
76000   76000   0   0
0           0   0   0
0           0   0   0
0           0   0   0
0           0   0   0
0           0   0   0
  • 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-17T15:17:13+00:00Added an answer on June 17, 2026 at 3:17 pm

    Nick makes a good point that your max call doesn’t reference the previous k, so it collapses to a check of the sixth column. Here’s the R-equivalent, assuming you really wanted the row maximum. I stored your data in a txt file first.

    data_all <- read.table("data.txt", header=T)
    data_one <- data_all[,1:6]
    data_two <- data_all[,7:12]
    my_fun_one <- function(x, y) {
      x * ((y == 1) | (y == 2))
    }
    data_three <- mapply(FUN = my_fun_one, x=data_one, y=data_two)
    my_fun_two <- function(x) {
      max(x, 0)
    }
    k <- apply(data_three, 1, FUN = my_fun_two)
    

    This yields

    > k
     [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5
    

    Update — here’s the solution to your updated, full problem. It uses, more or less, the same building blocks. Once you’re familiar with the basics of R, I think you will get the most mileage out of apply, lapply, and mapply.

    data_one <- read.table("data_one.txt", header=T)
    data_two <- read.table("data_two.txt", header=T)
    z1 <- data_one[, 1:6]
    z2 <- data_one[, 7:12]
    z3 <- data_two[, 1:6]
    z4 <- data_two[, 7:12]
    my_fun <- function(w, x, y, z) {
      z * (z > 0) * ((w %in% c(5, 10)) | (x %in% c(1, 2)) | (y == 1))
    }
    z5 <- mapply(FUN=my_fun, w=z1, x=z2, y=z3, z=z4)
    r <- rowSums(z5[, 1:3]) 
    s <- rowSums(z5[, 4:6]) 
    t <- rowSums(z5)
    k <- z5[, ncol(z5)]
    data_three <- data.frame(t, r, s, k)
    

    This yields:

    > data_three
           t     r s k
    1  81000 81000 0 0
    2  86000 86000 0 0
    3  96000 96000 0 0
    4  84000 84000 0 0
    5  76000 76000 0 0
    6      0     0 0 0
    7      0     0 0 0
    8      0     0 0 0
    9      0     0 0 0
    10     0     0 0 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: char stats[109]; /* !LINE UNDER QUESTION! */ sprintf(stats, OBJECTS:\n%u/256\n
I have the following code: function Person(){ this.age = 30; } function Stats(){ this.age
I am trying to integrate the following into my code to unregister phonestatelistener TelephonyManager
I'm trying to execute a query in PostgreSQL using the following code. It's written
I am just trying to implement the following code sample on our SQL Server
I'm trying to install code which I've successfully installed in the past on a
I have a list which looks like the following: <ul id=offers> <li class=unique-id-here> <span
I have the following code in my view <% foreach (var item in Model.stats)
hey I have been trying for hours and I don't understand why the following
I have a large block of code with several nested loops which needs 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.