use*_*432 72 string r uppercase
city,hs_cd,sl_no,col_01,col_02,col_03
Austin,1,2,,46,Female
Austin,1,3,,32,Male
Austin,1,4,,27,Male
Austin,1,5,,20,Female
Austin,2,2,,42,Female
Austin,2,1,,52,Male
Austin,2,3,,25,Male
Austin,2,4,,22,Female
Austin,3,3,,30,Female
Austin,3,1,,65,Female
Run Code Online (Sandbox Code Playgroud)
我想将数据框中的所有小写字符转换为大写.有没有办法一次性完成这一操作,而不是在每个字符变量上重复这样做?
jub*_*uba 77
从以下示例数据开始:
df <- data.frame(v1=letters[1:5],v2=1:5,v3=letters[10:14],stringsAsFactors=FALSE)
v1 v2 v3
1 a 1 j
2 b 2 k
3 c 3 l
4 d 4 m
5 e 5 n
Run Code Online (Sandbox Code Playgroud)
您可以使用 :
data.frame(lapply(df, function(v) {
if (is.character(v)) return(toupper(v))
else return(v)
}))
Run Code Online (Sandbox Code Playgroud)
这使 :
v1 v2 v3
1 A 1 J
2 B 2 K
3 C 3 L
4 D 4 M
5 E 5 N
Run Code Online (Sandbox Code Playgroud)
Tre*_*man 44
从dplyr包中,您还可以将mutate_all()函数与toupper()结合使用.这将影响字符和因子类.
library(dplyr)
df <- mutate_all(df, funs=toupper)
Run Code Online (Sandbox Code Playgroud)
LMc*_*LMc 13
以, ,_if
结尾的作用域动词已被1.0.0 或更高版本中的 所取代。为此,请使用:_at
_all
across()
packageVersion("dplyr")
across
df %>%
mutate(across(where(is.character), toupper))
Run Code Online (Sandbox Code Playgroud)
across
语法转换哪些列。上面的代码将在所有字符列上应用该函数。across
是要应用的函数。除了命名函数之外,它还支持 purrr 风格的 lambda 函数/公式语法:~ toupper(.x)
或匿名函数function(x) toupper(x)
(或 R > 4.1.0 中的简写\(x) toupper(x)
),使设置其他函数参数变得简单明了。或者,您可以使用包modify_*
中的函数purrr
:
df %>%
mutate(across(where(is.character), toupper))
Run Code Online (Sandbox Code Playgroud)
modify_if
是要应用的函数。除了命名函数之外,它还支持 purrr 风格的 lambda 函数/公式语法:~ toupper(.x)
或匿名函数function(x) toupper(x)
(或 R > 4.1.0 中的简写\(x) toupper(x)
),使设置其他函数参数变得简单明了。数据
df <- structure(list(city = c("Austin", "Austin", "Austin", "Austin",
"Austin", "Austin", "Austin", "Austin", "Austin", "Austin"),
hs_cd = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), sl_no = c(2L,
3L, 4L, 5L, 2L, 1L, 3L, 4L, 3L, 1L), col_01 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), col_02 = c(46L, 32L, 27L, 20L,
42L, 52L, 25L, 22L, 30L, 65L), col_03 = c("Female", "Male",
"Male", "Female", "Female", "Male", "Male", "Female", "Female",
"Female")), class = "data.frame", row.names = c(NA, -10L))
Run Code Online (Sandbox Code Playgroud)
在R中使用apply函数很简单
f <- apply(f,2,toupper)
Run Code Online (Sandbox Code Playgroud)
无需检查列是字符还是任何其他类型.
小智 7
另一种选择是使用mutate_if()
和str_to_upper()
函数的组合,两者都来自 tidyverse 包:
df %>% mutate_if(is.character, str_to_upper) -> df
Run Code Online (Sandbox Code Playgroud)
这会将数据框中的所有字符串变量转换为大写。
str_to_lower()
做相反的事情。
对于那些使用这些答案的人来说,这里有一个评论.朱巴的答案很棒,因为如果您的变量是数字或字符串,它是非常有选择性的.但是,如果你有一个组合(例如a1,b1,a2,b2)等,它将不会正确转换字符.
正如@Trenton Hoffman所说,
library(dplyr)
df <- mutate_each(df, funs(toupper))
Run Code Online (Sandbox Code Playgroud)
影响字符和因子类,适用于"混合变量"; 例如,如果您的变量同时包含字符和数字值(例如a1),则两者都将转换为因子.总的来说,这并不是一个太大的问题,但如果你最终想要匹配data.frames
df3 <- df1[df1$v1 %in% df2$v1,]
Run Code Online (Sandbox Code Playgroud)
其中df1已被转换并且df2包含未转换的data.frame或类似内容,这可能会导致一些问题.解决方法是你必须短暂运行
df2 <- df2 %>% mutate_each(funs(toupper), v1)
#or
df2 <- df2 %>% mutate_each(df2, funs(toupper))
#and then
df3 <- df1[df1$v1 %in% df2$v1,]
Run Code Online (Sandbox Code Playgroud)
如果您使用基因组数据,那么知道这可以派上用场.