[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.

相关推荐
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼2 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!3 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ3 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics4 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
yuanManGan6 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅886 小时前
XML内容解析成实体类
xml·java·开发语言
BillKu6 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript
Python自动化办公社区6 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
逐光沧海6 小时前
STL常用算法——C++
开发语言·c++