Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to iterate over multiple vectors

$
0
0

Fairly new to R. Been working on this for a while but can't seem to get it working. I am trying to create a column called peo$Tier. It has 3 levels, ("1", "2", "3")

tier1 <- c("ADP TOTAL SOURCE INC", "A 1 HR A DIVISION OF OASIS OUTSOURCING INC", "COADVANTAGE CORP", "INSPERITY INC", "OASIS ACQUISITION INC", "OASIS ACQUISITION INC A PAYCHEX CO", "OASIS DHR LLC", "OASIS OUTSOURCING CONTRACT II INC", "OASIS OUTSOURCING INC", "PAYCHEX BUSINESS SOLUTIONS LLC", "PAYCHEX HR OUTSOURCING LLC", "TRINET GROUP INC", "TRINET HR II HOLDINGS INC", "TRINET HR IV LLC")
tier2 <- c("ALLY HR LLC DBA MATRIXONESOURCE", "ALPHASTAFF GROUP INC", "CHOICE EMPLOYER SOLUTIONS INC", "CORNERSTONE CAPITAL GROUP INC", "DECISION HR", "FLORIDA RESOURCE MANAGEMENT LLC", "FRANKCRUM 2 INC", "IMPACT STAFF LEASING LLC", "JUSTWORKS EMPLOYMENT GROUP LLC", "KYMBERLY GROUP PAYROLL SOLUTIONS INC", "OCMI III INC DBA PEOPAYGO", "REGIS GROUP HOLDINGS INC", "SOUTH EAST PERSONNEL LEASING INC", "STAFFLINK OUTSOURCING INC", "THE S2 HR GROUP LLC", "TLR OF BONITA INC", "WORKFORCE BUSINESS SERVICES INC")

If peo$Tier variable is in vector tier1, "1" will be assigned. If peo$Tier variable is in vector tier2, "2" will be assigned. Else "3" will be assigned. These vector values come from another column called peo$NAMED.INSURED and there is a total of 208 unique character.

I tried subsetting but it only worked for the first value within the vector. I tried a for loop but getting the error:

longer object length is not a multiple of shorter object length

Not sure how to accomplish this task. Please help.

for (i in 1:length(peo$NAMED.INSURED)) {
    peo$Tier[i] <- ifelse(peo$NAMED.INSURED == tier1, 1, ifelse(peo$NAMED.INSURED == tier2, 2, 3))
}

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>