Replies: 5 comments 2 replies
-
|
survey <- read.csv("https://deepayan.github.io/BSDS/2024-01-DE/data/bsds-survey.csv") |
Beta Was this translation helpful? Give feedback.
-
Not sure if I understand the question correctly. Just fetching a random hobby from the vector and doing the calculations.survey <- read.csv("https://deepayan.github.io/BSDS/2024-01-DE/data/bsds-survey.csv") random_hobby <- sample(s, 1) String_counter <- function (n) String_counter(random_hobby) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
My answer for part (a) is `survey <- read.csv("https://deepayan.github.io/BSDS/2024-01-DE/data/bsds-survey.csv") t1<-gsub(",", "", s) |> tolower() |> trimws() countchars <- function(input_string) { for (char in strsplit(input_string, "")[[1]]) { return(count) c<-0 #H <- "Number Of Vowels in the sentence is/are " And my answer for part (b) is `survey <- read.csv("https://deepayan.github.io/BSDS/2024-01-DE/data/bsds-survey.csv") t1<-gsub(",", "", s) |> tolower() |> trimws() countVowels <- function(input_string) { for (char in strsplit(input_string, "")[[1]]) { return(count) c<-0 #H <- "Number Of Vowels in the sentence is/are " |
Beta Was this translation helpful? Give feedback.
-
i used a function called nchar() taught in tutorial classsurvey <- read.csv("https://deepayan.github.io/BSDS/2024-01-DE/data/bsds-survey.csv") hobbies <-tolower(hobbies) hobbies <- gsub("\.", "", hobbies) #removing punctuation marks hobbies <- strsplit(hobbies, " ") |> unlist() #splitting into individual words len <- function(h){ #for the distribution of length vow <- function(h){ #for the distribution of vowel } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This problem is motivated by a question in your Probability homework. Consider the random experiment of choosing a word uniformly randomly from a collection of words, and obtaining the distribution of (a) the length (number of characters) and (b) the number of vowels in the chosen word.
Here, we will consider a larger collection of words, where manual inspection is not practical. Specifically, we will use the responses about hobbies in the survey data collected from the class. The responses can be obtained as a vector of character strings as follows.
To answer (a) and (b), we would need to do several things first:
Once we do this, we will also need some way to count the number of characters in a word.
R has several functions for string manipulations. Here are some of them (for details, see their documentation, e.g.,
?ncharfor thenchar()function).gsub()Replaces characters or patterns by a replacement string.
tolower()Replaces characters by their lowercase versions.
trimws()Removes white space at the beginning or end of a string.
paste()Combines multiple strings together.
strsplit()Splits strings into parts by breaking at a pattern. This is a very useful function, but also a little complicated to use. A simple example is to split sentences on the space character as follows.
The result is a list, which a structure we have not yet learned about. A list is also a vector, but its elements can be arbitrary objects. In this case, each list element is a character vector (of different lengths). We will learn about lists in more detail later, but in this case, a function called
unlist()is enough to turn this list of character vectors back into a longer character vector of all words.Explore these functions, and any related functions you can find through the help system, and try to solve (a) and (b).
Feel free to seek any clarifications, or to share your ideas and solutions.
Beta Was this translation helpful? Give feedback.
All reactions