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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:13:53+00:00 2026-06-04T15:13:53+00:00

I have a data.frame with 6 columns. The first is for subjects, the second

  • 0

I have a data.frame with 6 columns. The first is for subjects, the second for blocks in an experiment, and columns 3,4 and 5 are values I need to calculate a binary score (0 or 1), that I want to add in the sixth column (that’s why now, it’s full of 0s).

head(kfdblock3to9)
    subject time        gr       ugr      sdugr IL
40002.3   40002    3 0.4475618 0.3706000 0.02994533  0
40002.4   40002    4 0.4361786 0.3901111 0.01846110  0
40002.5   40002    5 0.4279880 0.4550000 0.02811839  0
40002.6   40002    6 0.4313647 0.4134444 0.04352974  0
40002.7   40002    7 0.4420889 0.4394286 0.02883143  0
40002.8   40002    8 0.4325227 0.3960000 0.06559222  0

I’m trying to do this with a for loop, but I’m a beginner in R and I’m having difficulties with this. The scoring formula I’m trying to implement is one where:
If the value in column 3 ($gr) is less that the difference between the value in column 4 ($ugr) and .35 times the value in column 5 ($sdugr), then the subject receives a 1, otherwise a 0.

What I’ve tried so far is:

for (i in kfdblock3to9$subject) {
     if (kfdblock3to9$gr<(kfdblock3to9$ugr-(.35*kfdblock3to9$sdugr))) 
                 kfdblock3to9$IL=1
         else kfdblock3to9$IL=0
    }

This gives me 50 warnings, all saying:
“the condition has length > 1 and only the first element will be used”

I suppose I’m doing something wrong with the indexes then, but I haven’t been able to figure it out. Any help is much appreciated.

  • 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-04T15:13:55+00:00Added an answer on June 4, 2026 at 3:13 pm

    Take a look at within and ifelse :

    kfdblock3to9 <- 
    within(kfdblock3to9,
      IL <- ifelse( gr < ugr - 0.35 * dugr, 1, 0)
    )
    

    within() isn’t really that necessary, but it keeps your code a whole lot more readible and easier to understand.

    Why does it go wrong? That’s because your condition is vectorized : try

    kfdblock3to9$gr<(kfdblock3to9$ugr-(.35*kfdblock3to9$sdugr))
    

    and you will see it returns a logical vector. Now an if() clause can only deal with one boolean value at a time. If you have a vectorized result, you need a vectorized solution and that is ifelse()

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

Sidebar

Related Questions

I have a data frame with two columns. First column contains categories such as
I have a data frame where some of the columns contain NA values. How
Let's assume that we have a data frame x which contains the columns job
I have extracted data to a data frame with mixed format dates that need
I have a data frame the first columns of which are a sample ID
I have a data frame with 22239 rows & 200 columns. The first column
I have a data.frame with 20 columns. The first two are factors, and the
I have a large data frame that Im working with, the first few lines
I have two data frames with two columns each. The first column is timestamps
I have a data.frame with 2 columns: Node A, Node B. Each entry in

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.