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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:12:49+00:00 2026-05-14T06:12:49+00:00

(second question today – must be a bad day) I have a dataframe with

  • 0

(second question today – must be a bad day)

I have a dataframe with various columns, including a concentration column (numeric), a flag highlighting invalid results (boolean) and a description of the problem (character)

df <- structure(list(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), rawconc = c(77.4, 
52.6, 86.5, 44.5, 167, 16.2, 59.3, 123, 1.95, 181), reason = structure(c(NA, 
NA, 2L, NA, NA, NA, 2L, 1L, NA, NA), .Label = c("Fails Acceptance Criteria", 
"Poor Injection"), class = "factor"), flag = c("False", "False", 
"True", "False", "False", "False", "True", "True", "False", "False"
)), .Names = c("x", "rawconc", "reason", "flag"), row.names = c(NA, 
-10L), class = "data.frame")

I can create a column with the numeric level of the reason column

df$level<-as.numeric(df$reason)
df
    x rawconc                    reason  flag level
1   1   77.40                      <NA> False    NA
2   2   52.60                      <NA> False    NA
3   3   86.50            Poor Injection  True     2
4   4   44.50                      <NA> False    NA
5   5  167.00                      <NA> False    NA
6   6   16.20                      <NA> False    NA
7   7   59.30            Poor Injection  True     2
8   8  123.00 Fails Acceptance Criteria  True     1
9   9    1.95                      <NA> False    NA
10 10  181.00                      <NA> False    NA

and here’s what I want to do to create a column with ‘level’ many stars, but it fails

df$stars<-paste(rep("*",df$level)sep="",collapse="")
Error: unexpected symbol in "df$stars<-paste(rep("*",df$level)sep"

df$stars<-paste(rep("*",df$level),sep="",collapse="")
Error in rep("*", df$level) : invalid 'times' argument

rep("*",df$level)
Error in rep("*", df$level) : invalid 'times' argument

df$stars<-paste(rep("*",pmax(df$level,0,na.rm=TRUE)),sep="",collapse="")
Error in rep("*", pmax(df$level, 0, na.rm = TRUE)) : 
  invalid 'times' argument

It seems that rep needs to be fed one value at a time. I feel that this should be possible (and my gut says ‘use lapply’ but my apply fu is v. poor)

Any one want to try ?

  • 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-14T06:12:49+00:00Added an answer on May 14, 2026 at 6:12 am

    I think that you will need an apply-type function. This will work:

    df[is.na(df$level),"level"] <- 0
    df$level <- sapply(df$level, function(x) paste(rep("*",x),collapse=""))
    

    You would be better using sapply than lapply in this instance since it returns a vector instead of a list.

    From the help for rep:

    If ‘times’ consists of a single
    integer, the result consists of
    the whole input repeated this many times. If ‘times’ is a vector
    of the same length as ‘x’ (after replication by ‘each’), the
    result consists of ‘x[1]’ repeated ‘times[1]’ times, ‘x[2]’
    repeated ‘times[2]’ times and so on.

    One problem with using rep with a vector for the times parameter is that it just returns a vector and it discards instances when times=0. You can see this with this command: rep(rep("*", nrow(df)), times=df$level).

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

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This isn't too hard if you use the free Winforms… May 15, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer I suspect that you are using either @Cacheable or @Cache… May 15, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer Save all records to the database using myDate.ToUniversalTime() When loading… May 15, 2026 at 5:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.