[R] count the number of numeric columns: sapply & lapply

sapply

Question 7: The code for counting numerical variables is incorrect. You should use sapply and sum to count the number of numeric columns.

R 复制代码
num_vars <- sum(sapply(GE_survey, is.numeric))

sapply is a function in R that applies a specified function to each element of a list or vector and returns the results as a vector or list. It is a variant of the lapply function that simplifies the result to a vector or array if possible.

Here's an example of how you might use sapply differently:

Suppose you have a list of vectors representing the scores of students in different subjects:

R 复制代码
scores <- list(math=c(90, 85, 88), science=c(78, 82, 80), history=c(92, 88, 90))

You can use sapply to calculate the average score for each subject:

R 复制代码
avg_scores <- sapply(scores, mean)

In this example, sapply applies the mean function to each vector in the scores list and returns a named vector avg_scores containing the average score for each subject.

Another example could be checking if elements in a list are vectors:

R 复制代码
elements <- list(a=1:3, b="hello", c=matrix(1:4, nrow=2))
is_vector <- sapply(elements, is.vector)

Here, is_vector will be a logical vector indicating whether each element in elements is a vector (TRUE) or not (FALSE).

of course TRUE also means 1, you can use sum to get the total num of the vectors.

lapply

lapply is a function in R that applies a specified function to each element of a list or vector and returns the results as a list. It is similar to sapply, but lapply always returns a list, while sapply tries to simplify the result to a vector or array if possible.

Here's how you can use lapply:

Suppose you have a list of numbers and you want to calculate the square of each number. You can use lapply to apply the ^ (power) operator to each element of the list:

R 复制代码
numbers <- list(1, 2, 3, 4, 5)
squared_numbers <- lapply(numbers, function(x) x^2)

In this example, lapply applies the anonymous function function(x) x^2 to each element of the numbers list, returning a list of squared numbers.

You can also use lapply with named functions. For example, if you have a list of vectors and you want to calculate the mean of each vector, you can use the mean function:

R 复制代码
vectors <- list(a=c(1, 2, 3), b=c(4, 5, 6), c=c(7, 8, 9))
mean_values <- lapply(vectors, mean)

Difference

Let's delve deeper into the differences between lapply and sapply in terms of their return values and how they handle simplification:

  1. Return Value:

    • lapply: Always returns a list, even if the input is a vector. Each element of the list corresponds to the result of applying the function to each element of the input.
    • sapply: Tries to simplify the result. If the result is a list where**every element has the same length, it returns a matrix or array if the elements are atomic vectors (like numeric or character vectors),**or a list otherwise. If the result is a list of length 1, it returns a vector instead of a list.
  2. Handling of Simplification:

    • lapply: Does not attempt to simplify the result. It always returns a list, which can be useful when you want to maintain the structure of the input.
    • sapply: Attempts to simplify the result. If the result can be simplified to a vector or array, it returns that instead of a list, which can be more convenient in some cases.
  3. Limitations:

    • lapply: Since it always returns a list, you may need to use additional functions or methods to further process the result if you need a different data structure.
    • sapply: While it simplifies the result, it may not always simplify it in the way you expect. For example, if the input is a list of lists and the inner lists have different lengths, sapply will return a list instead of a matrix or array.

In summary, lapply is more consistent in its return value, always returning a list, which can be useful when you want to preserve the structure of the input. sapply, on the other hand, attempts to simplify the result, which can be convenient but may not always behave as expected, especially with nested or irregular data structures.

In R, a vector is a basic data structure that represents a sequence of elements of the same data type. Vectors can be of two types: atomic vectors and lists.

  1. Atomic Vectors : Atomic vectors can hold elements of one basic data type , such as logical, integer, numeric (double), character, or complex. For example, c(1, 2, 3, 4, 5) creates a numeric vector, and c("a", "b", "c") creates a character vector.

  2. Lists : Lists, on the other hand, can hold elements of different data types and can even contain other lists. Each element of a list can be of any type, including vectors or other lists. For example, list(1, "a", TRUE) creates a list with three elements of different types.

Differences:

  • Homogeneity: Vectors are homogeneous, meaning they can only contain elements of the same data type, while lists are heterogeneous, allowing elements of different types.
  • Accessing Elements : Elements of a vector can be accessed using indexing (e.g., my_vector[1]), while elements of a list can be accessed using either indexing or names (e.g., my_list[[1]] or my_list$name).
  • Storage : Vectors are more memory-efficientfor storing elements of the same type, as R can optimize the storage, while lists are more flexible but may consume more memory.
  • Operations : Vectors support vectorized operations, where operations are applied element-wise, while lists require more explicit iteration over elements.

In summary, vectors are simpler and more efficient for storing homogeneous sequences of data, while lists are more flexible and can store heterogeneous data structures.

相关推荐
晚夜微雨问海棠呀5 分钟前
长沙景区数据分析项目实现
开发语言·python·信息可视化
graceyun6 分钟前
C语言初阶习题【9】数9的个数
c语言·开发语言
波音彬要多做40 分钟前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
Swift社区1 小时前
Excel 列名称转换问题 Swift 解答
开发语言·excel·swift
一道微光1 小时前
Mac的M2芯片运行lightgbm报错,其他python包可用,x86_x64架构运行
开发语言·python·macos
矛取矛求1 小时前
QT的前景与互联网岗位发展
开发语言·qt
Leventure_轩先生1 小时前
[WASAPI]从Qt MultipleMedia来看WASAPI
开发语言·qt
向宇it1 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
是娜个二叉树!1 小时前
图像处理基础 | 格式转换.rgb转.jpg 灰度图 python
开发语言·python
Schwertlilien2 小时前
图像处理-Ch5-图像复原与重建
c语言·开发语言·机器学习