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
- R Hex to RGB converter
- t-stat for feature selection
- Could not find function “%<>%” with dplyr loaded
- Interpreting “condition has length > 1” warning from `if` function
- ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
- Error: could not find function “%>%”
- R on MacOS Error: vector memory exhausted (limit reached?)
- R programming: How do I get Euler’s number?
- adding x and y axis labels in ggplot2
- Set NA to 0 in R
- R: Using equation with natural logarithm in nls
- Counting the number of elements with the values of x in a vector
- Non-numeric Argument to Binary Operator Error in R
- What are the “standard unambiguous date” formats for string-to-date conversion in R?
- Error in Confusion Matrix : the data and reference factors must have the same number of levels
- Having trouble setting working directory
- How to find the statistical mode?
- Error in plot.window(…) : need finite ‘xlim’ values
- R ggplot2 scale_y_continuous : Combining breaks & limits
- Editing legend (text) labels in ggplot
- Add color to boxplot – “Continuous value supplied to discrete scale” error
- Extracting value specific rows in R
- case_when in mutate pipe
- Error in plot.window(…) : need finite ‘xlim’ valuescc
- Longer object length is not a multiple of shorter object length?
- plot.new has not been called yet
- Error in lm.fit(x,y,offset = offset, singular.ok,…) 0 non-NA cases with boxcox formula
- dim(X) must have a positive length when applying function in data frame
- Basic – T-Test -> Grouping Factor Must have Exactly 2 Levels
- Persistent invalid graphics state error when using ggplot2
- Error: attempt to use zero-length variable name
- Add text to ggplot
- How to debug “contrasts can be applied only to factors with 2 or more levels” error?
- R error: cannot coerce type ‘closure’ to vector of type ‘double’
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘y’, tried every possible way
- R t-test Grouping factor must have exactly 2 levels error
- remove legend title in ggplot
- Subscript out of bounds – general definition and solution?
- Plot a line graph, error in xy.coords(x, y, xlabel, ylabel, log) : ‘x’ and ‘y’ lengths differ
- Sum rows in data.frame or matrix
- Remove grid, background color, and top and right borders from ggplot2
- R: “Unary operator error” from multiline ggplot2 command
- invalid type (list) for variable
- not a Stata version 5-12 .dta file
- How to subset a Data frame column wise using column names?
- Create an empty data.frame
- MLE error in R: initial value in ‘vmmin’ is not finite
- No non-missing arguments warning when using min or max in reshape2
- Custom legend for multiple layer ggplot
- `fread` with headers with special characters (latin1) and unusual nested quotes
- How do you create vectors with specific intervals in R?
- How to move or position a legend in ggplot2
- Singularity in backsolve at level 0, block 1 in LME model
- “installation of package ‘FILE_PATH’ had non-zero exit status” in R
- Nested ifelse statement
- Convert categorical variables to numeric in R
- Plot multiple lines (data series) each with unique color in R
- Plot two graphs in same plot in R
- How to overcome error:”attempt to set ‘colnames’ on an object with less than two dimension” in xts object
- How to get summary statistics by group
- Run R script from command line
- How to avoid “Error in stripchart.default(x1, …) : invalid plotting method” error?
- ‘height’ must be a vector or a matrix. barplot error
- What causes an R script to get Killed?
- What’s the difference between facet_wrap() and facet_grid() in ggplot2?
- ggplot2, facet_grid, free scales?
- dplyr mutate with conditional values
- How do I install an R package from source?
- Getting invalid model formula in ExtractVars when using rpart function in R
- R Error (from NA’s to 0): duplicate subscripts for column in Data Frame
- Error in file(filename, “r”, encoding = encoding) : cannot open the connection
- incorrect number of subscripts on matrix in R
- Error in rbind(deparse.level, …) : numbers of columns of arguments do not match R
- Correct syntax for mutate_if
- Plotting data in R; error: default method not implemented for type ‘list’
- Error:attempt to apply non-function
- R apply function with multiple parameters
- Changing fonts in ggplot2
- Error in
: target of assignment expands to non-language object - run a for loop in parallel in R
- Error in na.fail.default: missing values in object – but no missing values
- Error: Invalid number of ‘breaks’ in R
- Efficiently sum across multiple columns in R
- How to eliminate “NA/NaN/Inf in foreign function call (arg 7)” running predict with randomForest
- Sample random rows in dataframe
- Error in eval(predvars, data, env) : object ‘Rm’ not found
- How to convert data.frame column from Factor to numeric [duplicate]
- Error in x[[i]] : recursive indexing failed at level 2
- Merge error : negative length vectors are not allowed
- non-numeric argument to binary operator
- How to avoid “operations are possible only for numeric, logical or complex types” when computing top 3 elements in each group
- Column standard deviation R [duplicate]
- why nrow(dataframe) and length(dataframe) in r give different results?
- Meaning of objects being masked by the global environment
- Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt
- cbind warnings : row names were found from a short variable and have been discarded
- How can I change the name of a data frame
- How to calculate combination and permutation in R?
- In R, dealing with Error: ggplot2 doesn’t know how to deal with data of class numeric