Problem while computing `data = map(.data$data, .f, ...)`.

Hello, I am new to R and trying to run this code in Rgui console

library(dplyr)
library(rstatix)

ctrl_list <- 5
comp_list <- 4
values_val <- "737704.91803, 708661.41732, 1298701.29870, 1215066.82868, 750000.00000, 74074074.07407, 75757575.75758, 74074074.07407, 68965517.24138"

names_val <- c()
for (i in 1:length(ctrl_list)) {
    for (j in 1:ctrl_list[i]) {
        names_val <- c(names_val, paste("Ctrl", i, sep = ""))
    }
}

for (i in 1:length(comp_list)) {
    for (j in 1:comp_list[i]) {
        names_val <- c(names_val, paste("Comp", i, sep = ""))
    }
}
SSParameter <- data.frame(
    Population = names_val,
    ThresholdVoltage = values_val
)

ctrl_names_ss <- c()

for (i in 1:ctrl_list) {
    ctrl_names_ss <- c(ctrl_names_ss, paste("Ctrl", i, sep = ""))
}

comp_names_ss <- c()

for (i in 1:comp_list) {
    comp_names_ss <- c(comp_names_ss, paste("Comp", i, sep = ""))
}

SSParameter$Population <- factor(SSParameter$Population, levels = c(ctrl_names_ss, comp_names_ss))
SSParameter <- group_by(SSParameter, Population)
# Normality
return_val <- shapiro_test(SSParameter, ThresholdVoltage)
c(return_val$p[1], return_val$p[2])

but I get these errors

Hello,

could you explain a bit more what you do in the first part? You initialise two numeric values, ctrl_list and comp_list with 5 and 4 respectively. Then you create (in a rather strange way) a chr vector with length 5 + 4 containing "Ctrl1 and "Comp1" as entries. And then you asign those to a column in a data.frame with ALL numbers from your chr vector vales_val as entry of ThresholdVoltage.

There is one main problem you have:

  • there are no numeric values in your data.frame and you cannot test normality on "words"

Assuming there are reasons for the given input (e.g. the strange chr vector of numeric values and your for loops), you can do the following:

library(dplyr)
library(rstatix)

# define given variables
ctrl_list <- 5
comp_list <- 4
values_val <- "737704.91803, 708661.41732, 1298701.29870, 1215066.82868, 750000.00000, 74074074.07407, 75757575.75758, 74074074.07407, 68965517.24138"

# names_val <- c()
# better way of initializing a vector
names_val <- vector(length = 0L)
for (i in 1:length(ctrl_list)) {
  for (j in 1:ctrl_list[i]) {
    names_val <- c(names_val, paste("Ctrl", i, sep = ""))
  }
}

for (i in 1:length(comp_list)) {
  for (j in 1:comp_list[i]) {
    names_val <- c(names_val, paste("Comp", i, sep = ""))
  }
}
###
# Split the values vector
correct_values <- as.numeric(unlist(strsplit(values_val, split = ",")))

# define the data.frame
SSParameter <- data.frame(
  Population = names_val,
  ThresholdVoltage = correct_values
)

ctrl_names_ss <- c()

for (i in 1:ctrl_list) {
  ctrl_names_ss <- c(ctrl_names_ss, paste("Ctrl", i, sep = ""))
}

comp_names_ss <- c()

for (i in 1:comp_list) {
  comp_names_ss <- c(comp_names_ss, paste("Comp", i, sep = ""))
}

# initialize the factor
SSParameter$Population <- factor(SSParameter$Population,
                                 levels = c(ctrl_names_ss, comp_names_ss))
SSParameter <- group_by(SSParameter, Population)

# do the Shapiro Wilk test on normality
return_val <- shapiro_test(SSParameter, ThresholdVoltage)
c(return_val$p[1], return_val$p[2])
#> [1] 0.04403713 0.18992916

Created on 2022-08-29 by the reprex package (v2.0.1)

Hope this already solves your issue.

Kind regards

1 Like

@FactOREO Thank you very much, this was exactly the right answer I needed

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.