Question: Write down the R code for the following question ## 4) Using Apply Apply functions are useful when you want to apply a certain operation

Write down the R code for the following question

Write down the R code for the following question ## 4) Using

## 4) Using Apply Apply functions are useful when you want to apply a certain operation to all the rows of a list or dataframe. In the example below, we will only consider the case of a data frame. Note that this idea would be useful for your next lab. Here we apply str_locate() to locate area code from phone numbers. Using the output of str_locate() , we could then extract the area code. 503 phone_num = c( "401-501-1111", "(401)501-1111", "401 501 1111", "401-5011111", "+408-501-1111") code_pos = lapply( phone_num, function(x) str_locate(x, pattern = regex(' [0-9] {3} [- )]*')>) # Use substring() to extract the area code by sharing the start # and end position with the function as arguments. code_pos = do. call(rbind, code_pos) substring(phone_num, code_pos[, 1], code_pos [, 2]-1) ### 4. 1) Your Turn Using the idea above, from the nba dataframe, pull the last names of all NBA players. Find the frequencies of all the last names. _Hint: _ Use 'str_locate() and `substring(). Eg: str_locate(nba$player, ...). Alternatively, you may choose to try this with Lapply() but this is slightly tricky. # your code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!