Dee*_*ena 14 r apply dataframe
我试图将一些转换应用于数据帧中的所有元素.
使用常规应用函数时,我得到一个矩阵,而不是数据帧.有没有办法直接获取数据帧而不添加as.data.frame
到每一行?
df = data.frame(a = LETTERS[1:5], b = LETTERS[6:10])
apply(df, 1, tolower) #Matrix
apply(df, 2, tolower) #Matrix
sapply(df, tolower) #Matrix
as.data.frame(sapply(df, tolower)) # Can I avoid "as.data.frame"?
Run Code Online (Sandbox Code Playgroud)
akr*_*run 21
我们可以使用lapply
并将其分配回'df'
df[] <- lapply(df, tolower)
Run Code Online (Sandbox Code Playgroud)
在[]
保留了相同的结构原始数据集.使用apply
将其转换为a matrix
并且不建议使用.
这是一种使用方式dplyr
:
library(dplyr)
df %>% mutate_each(funs(tolower))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13720 次 |
最近记录: |