[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 分钟前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机
LallanaLee5 分钟前
常见面试题
java·开发语言
酷炫码神10 分钟前
C#运算符
开发语言·c#
小秋学嵌入式-不读研版14 分钟前
C42-作业练习
c语言·开发语言·笔记
休息一下接着来21 分钟前
C++ 条件变量与线程通知机制:std::condition_variable
开发语言·c++·算法
小哈里41 分钟前
【pypi镜像源】使用devpi实现python镜像源代理(缓存加速,私有仓库,版本控制)
开发语言·python·缓存·镜像源·pypi
努力学习的小廉1 小时前
【C++】 —— 笔试刷题day_29
开发语言·c++·算法
电商数据girl1 小时前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
天天打码1 小时前
python版本管理工具-pyenv轻松切换多个Python版本
开发语言·python
CircleMouse1 小时前
基于 RedisTemplate 的分页缓存设计
java·开发语言·后端·spring·缓存