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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:53+00:00 2026-05-26T14:41:53+00:00

I am puzzled by this behavior in R. I just want to do a

  • 0

I am puzzled by this behavior in R. I just want to do a simple string compare of a list of strings produced by strsplit. So do not understand why the following first two code pieces do what I expected, while the third is not.

> for (i in strsplit("A text I want to display with spaces", " ")) { print(i) }
[1] "A"       "text"    "I"       "want"    "to"      "display" "with"    "spaces" 

Ok, this makes sense …

> for (i in strsplit("A text I want to display with spaces", " ")) { print(i=="want") }
[1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE

Ok, this too. But, what is wrong with the following construction?

> for (i in strsplit("A text I want to display with spaces", " ")) { if (i=="want")     print("yes") }
Warning message:
In if (i == "want") print("yes") :
  the condition has length > 1 and only the first element will be used

Why doesn’t this just print “yes” when the fourth word is encountered? What should I change to have this desired behavior?

  • 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-26T14:41:54+00:00Added an answer on May 26, 2026 at 2:41 pm

    The problem is that strsplit produces a list of split strings (in this case with length 1, because you only gave it a single string to split).

    ss <- strsplit("A text I want to display with spaces", " ")
    for (i in ss[[1]]) {
      if (i=="want")     print("yes")
    }
    

    You can see what’s going on if you just print the elements:

    for (i in ss) {
      print(i)
    }
    

    the first element is a character vector.

    Depending on what you’re doing you might also consider vectorized comparisons such as ifelse(ss=="want","yes","no")

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

Sidebar

Related Questions

I am a bit puzzled by this strange behavior on CentOs 5.4 when starting
I'm just puzzled with this one, it's a Haskell loop-sort-of-thing which I can't figure
okay...im really puzzled with this. I want to create a regular asp.net webforms website
I'm really puzzled by this one!! I'm sure it's simple but really can't figure
I just noticed a behavior in argparse that puzzled me (guess I'd never used
I am puzzled by the following behavior of Function : In[1]:= InlineCellInMessage=Function[expr,DisplayForm[Cell[BoxData[MakeBoxes[expr,StandardForm]],Input]],{HoldAllComplete}] Out[1]= Function[expr,MakeBoxes[expr,StandardForm]]
I've been a little puzzled with this as I have not seen many examples
Really puzzled on this, probably simple but it is the day after New Years.
I'm quite puzzled by this one, getting this error from firefox. escape is not
I am really puzzled by this. I believe I am managing memory the correct

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.