concept new variable in category R

This is an excerpt from Manning's book R in Action, Third Edition.
The sqlQuery() function is powerful because any valid SQL statement can be inserted. This flexibility allows you to select specific variables, subset the data, create new variables, and recode and rename existing variables.
3.2 Creating new variables
In a typical research project, you’ll need to create new variables and transform existing ones. This is accomplished with statements of the form
the statements will succeed but you’ll end up with a data frame (leadership) and two separate vectors (total_score and mean_score). This probably isn’t the result you want. Ultimately, you want to incorporate new variables into the original data frame. The following listing provides two separate ways to accomplish this goal. The one you choose is up to you; the results will be the same.
Listing 3.2 Creating new variables
leadership$total_score <- leadership$q1 + leadership$q2 + leadership$q3 + leadership$q4 + leadership$q5 leadership$mean_score <- (leadership$q1 + leadership$q2 + leadership$q3 + leadership$q4 + leadership$q5)/5 leadership <- transform(leadership, total_score = q1 + q2 + q3 + q4 + q5, mean_score = (q1 + q2 + q3 + q4 + q5)/5)

This is an excerpt from Manning's book Exploring Data with R.
mutate() — Create new variables with functions of existing variables
Listing 3.24 Modifying `dmd` to add three new columns, and using plotting `dmd_mod` with the new variables.
```{r dmd_modified_dplyr_ggplot} dmd_mod <- //#A dmd %>% mutate(cpc = price / carats) %>% mutate(price_class = ifelse(cpc >= 3460, "Above Median", "Below Median")) %>% mutate(quality = ifelse( cut == "The Best" & clarity == "The Best", "Top Drawer", "The Rest") ) ggplot(dmd_mod, aes(x = carats, y = price)) + //#B geom_point() + facet_wrap( facets = vars(price_class, quality), labeller = label_both ) + labs(x = "Carats", y = "Price") ```

This is an excerpt from Manning's book R in Action, Second Edition: Data analysis and graphics with R.
The sqlQuery() function is powerful because any valid SQL statement can be inserted. This flexibility allows you to select specific variables, subset the data, create new variables, and recode and rename existing variables.
In a typical research project, you’ll need to create new variables and transform existing ones. This is accomplished with statements of the form
the statements will succeed but you’ll end up with a data frame (mydata) and two separate vectors (sumx and meanx). This probably isn’t the result you want. Ultimately, you want to incorporate new variables into the original data frame. The following listing provides three separate ways to accomplish this goal. The one you choose is up to you; the results will be the same.
Listing 4.2. Creating new variables
mydata<-data.frame(x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8)) mydata$sumx <- mydata$x1 + mydata$x2 mydata$meanx <- (mydata$x1 + mydata$x2)/2 attach(mydata) mydata$sumx <- x1 + x2 mydata$meanx <- (x1 + x2)/2 detach(mydata) mydata <- transform(mydata, sumx = x1 + x2, meanx = (x1 + x2)/2)