1

Problem: Code will not run past ##ERROR FROM HERE. After setting the vetor to 100 iterations of 40 words, I think r is struggling to mutate the words in ctrl_d and run the formula on them (word*1000/ dictionary size). Could any advise as to why this is? In the error code below, 'stretch' is one of the 40 random words in ctrl_d dataframe.

R error output: Error: Problem with mutate() input stretch. x non-numeric argument to binary operator i Input stretch is unit * stretch/dictsize.

Context code:

#new control data frame which removes 16 starting rain tokens
nouncount_ctrl <- nouncount %>% select(-matches(rainset_regex))
  
#1) restrict dictionaries to total noun counts of 1000 only 
nouncount_ctrl <- nouncount_ctrl %>% filter(total_ncount >= 1000)

#convert 0 values to NA
nouncount_ctrl[nouncount_ctrl == 0] <- NA

#2) filter for appearance of tokens in more than 5% of dictionaries (words must appear in >97 dictionaries) 
nouncount_ctrl = nouncount_ctrl[,!sapply(nouncount_ctrl, function(x) mean(is.na(x)))>0.95]

#generate vector for number of control trials
vector <- c(1:100)

#package to print correlation statistics
magic_for(print, silent = TRUE)

#loop control analysis, iterating for vector length
for(number in vector){
#select random words
  random_words <- colnames(sample(select(nouncount_ctrl, 11:3240), size=40))
  
#generate initial control dataframe
ctrl_d <- nouncount_ctrl %>% 
  select(id, random_words, dictsize) %>% mutate_all(~replace_na(.x, 0))

unit <- 1000

Code causing error:

# mutate relative frequencies
for (word in random_words){
  ctrl_d <- ctrl_d %>% mutate(!!sym(word) :=  unit * !!sym(word)/dictsize)
}

n.baes
  • 19
  • 3
  • Possible duplicate: https://stackoverflow.com/q/29665428/680068 – zx8754 Sep 23 '20 at 10:08
  • Check if the linked post helps, it is hard to debug without reproducible example data. And try to minimise your code, keep only relevant bits that cause the error: for example, ggplot bit is not relevant, delete it. – zx8754 Sep 23 '20 at 10:11
  • I don't see `unit` defined anywhere before that critical line. If it used to be a column in your `nouncount_ctrl` dataframe, make sure to include it in the `select` statement when you create `ctrl_d` – alex_jwb90 Sep 23 '20 at 10:22
  • 1
    @zx8754 thank you for the tips, hopefully I have improved the question above. I think the problem may be similar to the linked post, albeit different. I will keep going back as I troubleshoot. – n.baes Sep 23 '20 at 10:37
  • @ alex_jwb90 thank you. I did not have it in a frame, but in a variable. I have included `unit` in `ctrl_d` by piping it through with *%>% mutate(unit = unit) %>%* ... this has now changed the error code in the same place as above to: **Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "character"** – n.baes Sep 23 '20 at 10:38

0 Answers0