I think you’re asking how to compute the mean of a variable in a data frame, given the name of the column. There are two typical approaches to doing this, one indexing with [[
and the other indexing with [
:
data(iris) mean(iris[["Petal.Length"]]) # [1] 3.758 mean(iris[,"Petal.Length"]) # [1] 3.758 mean(iris[["Sepal.Width"]]) # [1] 3.057333 mean(iris[,"Sepal.Width"]) # [1] 3.057333
Related Posts:
- Mean of a column in a data frame, given the column’s name
- Poker hand range chart visualization in R
- Emulate ggplot2 default color palette
- ggplot wrong color assignment
- How to rename a single column in a data.frame?
- Critical t values in R
- Error in
: object of type ‘closure’ is not subsettable - How to coerce a list object to type ‘double’
- R Error in x$ed : $ operator is invalid for atomic vectors
- $ operator is invalid for atomic vectors for dataframe R
- ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
- How to open CSV file in R when R says “no such file or directory”?
- What does na.rm=TRUE actually means?
- Remove legend ggplot 2.2
- How do I replace NA values with zeros in an R dataframe?
- R on MacOS Error: vector memory exhausted (limit reached?)
- Why use as.factor() instead of just factor()
- Update R using RStudio
- mean() warning: argument is not numeric or logical: returning NA
- ‘x’ and ‘y’ lengths differ ERROR when plotting
- Difference between paste() and paste0()
- Why do I get “number of items to replace is not a multiple of replacement length”
- Drop data frame columns by name
- How can two strings be concatenated?
- Error in ggplot.data.frame : Mapping should be created with aes or aes_string
- SummarySE (Rmisc package) to produce a barplot with error bars (ggplot2)
- Error in Confusion Matrix : the data and reference factors must have the same number of levels
- Remove a row from a data table in R
- Error in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric, What should I do in this situation?
- “Error: Mapping should be created with `aes()` or `aes_()`.”
- Principal Components Analysis:Error in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric
- “Error: Continuous value supplied to discrete scale” in default data set example mtcars and ggplot2
- Why am I getting “algorithm did not converge” and “fitted prob numerically 0 or 1” warnings with glm?
- How does one reorder columns in a data frame?
- Arcsine transformation of percentage data
- R loop that adds following numbers to each element in list
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) 0 non-na cases
- Can I calculate z-score with R?
- Using Caret Package but Getting Error in library(e1071)
- Function to clear the console in R and RStudio
- plot.new has not been called yet
- R: need finite ‘ylim’ values in function
- How to save a data frame as CSV to a user selected location using tcltk
- Convert a list to a data frame
- Why I get this error writing data to a file
- Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code
- Remove NA values from a vector
- character string is not in a standard unambiguous format
- Remove rows with all or some NAs (missing values) in data.frame
- duplicate ‘row.names’ are not allowed error
- Adding a regression line on a ggplot
- How to count the number of observations in R like Stata command count
- What does the double percentage sign (%%) mean?
- Error: unexpected ‘}’ in “}” in R [duplicate]
- Error : length of ‘dimnames’ [2] not equal to array extent
- Error in table “all arguments must have the same length”
- Rename multiple columns by names
- Remove rows with all or some NAs (missing values) in data.frame
- Subscript out of bounds – general definition and solution?
- How to not run an example using roxygen2?
- file.path function in R
- Increase number of axis ticks
- Reshaping data.frame from wide to long format
- Converting data frame column from character to numeric
- How to assign colors to categorical variables in ggplot2 that have stable mapping?
- How to set limits for axes in ggplot2 R plots?
- What is the difference between np.mean and tf.reduce_mean?
- duplicate ‘row.names’ are not allowed error
- Error: invalid subscript type ‘list’ in R
- “non-numeric argument to binary operator” error in R
- What does “The following object is masked from ‘package:xxx'” mean?
- grep using a character vector with multiple patterns
- dcast warning: ‘Aggregation function missing: defaulting to length’
- remove all variables except functions
- install.packages fails in knitr document: “trying to use CRAN without setting a mirror”
- Non-conformable arrays error in code
- How to convert a data frame column to numeric type?
- Boxplot dplyr: Error: non-numeric argument to binary operator
- Function to calculate R2 (R-squared) in R
- Too few periods for decompose()
- lmer error: grouping factor must be < number of observations
- Aggregate multiple columns at once
- installing data.table results in “there is no package”
- R histogram range error: some ‘x’ not counted; maybe ‘breaks’ do not span range of ‘x
- What is the meaning of the dollar sign “$” in R function()?
- How to split data into training/testing sets using sample function
- How do you specifically order ggplot2 x axis instead of alphabetical order?
- IF “OR” multiple conditions
- R4DS error comparison (1) is possible only for atomic and list types
- How to initialize a vector with fixed length in R
- Efficiently sum across multiple columns in R
- Convert the values in a column into row names in an existing data frame
- How can I make a list of lists in R?
- lib unspecified & Error in loadNamespace
- Error Error in storage.mode(x) <- "double" : 'list' object cannot be coerced to type 'double'
- Warning: non-integer #successes in a binomial glm! (survey packages)
- Remove columns from dataframe where some of values are NA
- Subset and ggplot2
- What is the meaning of the dollar sign “$” in R function()?
- invalid (do_set) left-hand side to assignment in R