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

相关推荐
_.Switch10 分钟前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
醉颜凉13 分钟前
银河麒麟桌面操作系统修改默认Shell为Bash
运维·服务器·开发语言·bash·kylin·国产化·银河麒麟操作系统
NiNg_1_23418 分钟前
Vue3 Pinia持久化存储
开发语言·javascript·ecmascript
带带老表学爬虫27 分钟前
java数据类型转换和注释
java·开发语言
qianbo_insist30 分钟前
simple c++ 无锁队列
开发语言·c++
BigYe程普40 分钟前
我开发了一个出海全栈SaaS工具,还写了一套全栈开发教程
开发语言·前端·chrome·chatgpt·reactjs·个人开发
彭于晏68942 分钟前
Android广播
android·java·开发语言
弱冠少年1 小时前
websockets库使用(基于Python)
开发语言·python·numpy
长天一色1 小时前
C语言日志类库 zlog 使用指南(第五章 配置文件)
c语言·开发语言
一般清意味……1 小时前
快速上手C语言【上】(非常详细!!!)
c语言·开发语言