Watts-College / cpp-527-spr-2022

https://watts-college.github.io/cpp-527-spr-2022/
0 stars 1 forks source link

Lab 3 - Q-1 #22

Open mmonakho opened 2 years ago

mmonakho commented 2 years ago

I am trying to calculate the number of power lists titles that start with a number (1, 2, 3, etc. as well as "One", "Two", "Three", etc.) I calculatd the number of titles that start with an actual number but I'm struggling with the ones that have numbers written with words. I tried this: numbers <- c("One","Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten") sum(grepl(numbers, titles)) But it said: Warning message: In grepl(numbers, titles) : argument 'pattern' has length > 1 and only the first element will be used Not sure hot to deal with this without using stringr package. Tried replacing the words with numbers... gsub( pattern="one", replacement="1", titles) gsub( pattern="two", replacement="2", titles) gsub( pattern="three", replacement="3", titles) gsub( pattern="four", replacement="4", titles) gsub( pattern="five", replacement="5", titles) gsub( pattern="six", replacement="6", titles) gsub( pattern="seven", replacement="7", titles) gsub( pattern="eight", replacement="8", titles) gsub( pattern="nine", replacement="9", titles) gsub( pattern="ten", replacement="10", titles) ...but the output number did not change.

Dselby86 commented 2 years ago

The Stringer package is a fine answer to this problem. THe only thing I would point out about your code is that after 4 you go back to referencing 1 repeatedly.

On Tue, Feb 1, 2022 at 4:13 PM mmonakho @.***> wrote:

I am trying to calculate the number of power lists titles that start with a number (1, 2, 3, etc. as well as "One", "Two", "Three", etc.) I calculatd the number of titles that start with an actual number but I'm struggling with the ones that have numbers written with words. I tried this: numbers <- c("One","Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten") sum(grepl(numbers, titles)) But it said: Warning message: In grepl(numbers, titles) : argument 'pattern' has length > 1 and only the first element will be used

Not sure hot to deal with this without using stringr package. Tried replacing the words with numbers... gsub( pattern="one", replacement="1", titles) gsub( pattern="two", replacement="2", titles) gsub( pattern="three", replacement="3", titles) gsub( pattern="four", replacement="4", titles) gsub( pattern="five", replacement="1", titles) gsub( pattern="six", replacement="1", titles) gsub( pattern="seven", replacement="1", titles) gsub( pattern="eight", replacement="1", titles) gsub( pattern="nine", replacement="1", titles) gsub( pattern="ten", replacement="1", titles) ...but the output number did not change.

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/22, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHB2ZLKQKXLBX2H6XOODUZBSKDANCNFSM5NKQN7EQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

dholford commented 2 years ago

I also did the longer route. I wonder, though, if the problem is when you wrote out the word you went lower case. Since Power List is based on the first word of the title, it's most likely going to be capitalized. And, since you are only worried about the first word anyone, you could just sub ( ) as opposed to gsub( ). That seemed to work for me:

d$title <- sub("One", "1", sub("Two", "2", sub("Three", "3", sub("Four", "4", sub("Five", "5", sub("Six", "6", sub("Seven", "7", sub("Eight", "8", sub("Nine", "9", sub("Ten", "10", d$title))))))))))