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

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:

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:

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:

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:

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:

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-efficient for 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.

你可能感兴趣的:(R,r语言,开发语言)