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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:16:05+00:00 2026-06-12T08:16:05+00:00

I am attempting to set a column in a dataframe equal to either ‘US’

  • 0

I am attempting to set a column in a dataframe equal to either ‘US’ or ‘Foreign’, depending on country. I believe the proper way to do so is to write a function, then use sapply to actually update the dataframe. This is the first time I’ve attempted something like this in R – in SQL, I would have just written an UPDATE query.

Here is my dataframe:

str(clients)
'data.frame':   252774 obs. of  4 variables:
 $ ClientID     : Factor w/ 252774 levels "58187855","59210128",..: 19 20 21 22 23 24 25 26 27 28 ...
 $ Country          : Factor w/ 207 levels "Afghanistan",..: 196 60 139 196 196 40 40 196 196 196 ...
 $ CountryType     : chr  "" "" "" "" ...
 $ OrderSize        : num  12.95 21.99 5.00 7.50 44.5 ...


head(clients)
       ClientID  Country       CountryType  OrderSize
1      58187855  United States              12.95
2      59210128  France                     21.99
3      65729284  Pakistan                   5.00
4      25819711  United States              7.50
5      62837458  United States              44.55
6      88379852  China                      99.28

The function I attempted to write is this:

updateCountry <- function(x) {
  if (clients$Country == "US") {
        clients$CountryType <- "US"
  } else {
    clients$CountryType <- "Foreign"
    }
}

I would then apply it like so:

sapply(clients, updateCountry)

When I run sapply against the head of the dataframe, I get this:

"US" "US" "US" "US" "US" "US" 
Warning messages:
1: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
2: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
3: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
4: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
5: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
6: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used

It appears that the function is classifying the Country correctly, but is not updating the clients$CountryType column correctly. What am I doing wrong? Also – is this the best way to accomplish updating the dataframe?

  • 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-12T08:16:07+00:00Added an answer on June 12, 2026 at 8:16 am

    ifelse seems like what you actually want here. It’s a vectorized version of the if/else construct.

     clients$CountryType <- ifelse(clients$Country == "US", "US", "Foreign")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create a data set from an original dataframe (in either
The Set Up: Loading a DataReader into a DataTable. Attempting to alter a column
Attempting to set up an automated texting service for customers, where people can text
I'm attempting to set up Pinned Site features for my project. The trouble is
I'm attempting to set up the Laravel PHP Framework to work with Nginx .
I am attempting to set up my own project using a third party library
I am new to all things maven and ignition. I am attempting to set
Possible Duplicate: C# generic constraint for only integers Greets! I'm attempting to set up
I am attempting to create a set of folders that comes in from a
I am attempting to run the following command in KornShell (ksh): set -A INDEXES

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.