데이터 프레임의 모든 문자 변수에있는 모든 값을 소문자에서 대문자로 변환
문자 및 숫자 변수 의 혼합 데이터 프레임 이 있습니다.
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
데이터 프레임의 모든 소문자를 대문자로 변환하고 싶습니다. 각 캐릭터 변수에 대해 반복하지 않고 한 번에이 작업을 수행 할 수있는 방법이 있습니까?
다음 샘플 데이터로 시작합니다.
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
당신이 사용할 수있는 :
data.frame(lapply(df, function(v) {
if (is.character(v)) return(toupper(v))
else return(v)
}))
제공하는 :
v1 v2 v3
1 A 1 J
2 B 2 K
3 C 3 L
4 D 4 M
5 E 5 N
dplyr 패키지에서 toupper ()와 함께 mutate_all () 함수를 사용할 수도 있습니다. 이는 캐릭터 및 요인 클래스 모두에 영향을 미칩니다.
library(dplyr)
df <- mutate_all(df, funs=toupper)
R의 적용 기능으로 간단합니다.
f <- apply(f,2,toupper)
열이 문자인지 다른 유형인지 확인할 필요가 없습니다.
이 답변 중 하나를 사용하는 사람들을 위해 여기에 추가 의견이 있습니다. 변수가 숫자 또는 문자열이면 매우 선택 적이기 때문에 Juba의 대답은 훌륭합니다. 그러나 조합 (예 : a1, b1, a2, b2)이있는 경우 문자가 제대로 변환되지 않습니다.
@Trenton Hoffman이 언급했듯이
library(dplyr)
df <- mutate_each(df, funs(toupper))
문자 및 요인 클래스 모두에 영향을 미치고 "혼합 변수"에 대해 작동합니다. 예를 들어 변수에 문자와 숫자 값 (예 : a1)이 모두 포함되어 있으면 둘 다 요인으로 변환됩니다. 전반적으로 이것은 큰 문제는 아니지만 예를 들어 일치하는 data.frames를 원할 경우
df3 <- df1[df1$v1 %in% df2$v1,]
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,]
게놈 데이터로 작업하는 경우 이것이 유용 할 수 있음을 알 때입니다.
If you need to deal with data.frames that include factors you can use:
df = data.frame(v1=letters[1:5],v2=1:5,v3=letters[10:14],v4=as.factor(letters[1:5]),v5=runif(5),stringsAsFactors=FALSE)
df
v1 v2 v3 v4 v5
1 a 1 j a 0.1774909
2 b 2 k b 0.4405019
3 c 3 l c 0.7042878
4 d 4 m d 0.8829965
5 e 5 n e 0.9702505
sapply(df,class)
v1 v2 v3 v4 v5
"character" "integer" "character" "factor" "numeric"
Use mutate_each_ to convert factors to character then convert all to uppercase
upper_it = function(X){X %>% mutate_each_( funs(as.character(.)), names( .[sapply(., is.factor)] )) %>%
mutate_each_( funs(toupper), names( .[sapply(., is.character)] ))} # convert factor to character then uppercase
Gives
upper_it(df)
v1 v2 v3 v4
1 A 1 J A
2 B 2 K B
3 C 3 L C
4 D 4 M D
5 E 5 N E
While
sapply( upper_it(df),class)
v1 v2 v3 v4 v5
"character" "integer" "character" "character" "numeric"
Another alternative is to use a combination of mutate_if() and str_to_uper() function, both from the tidyverse package:
df %>% mutate_if(is.character, str_to_upper) -> df
This will convert all string variables in the data frame to upper case. str_to_lower() do the opposite.
Alternatively, if you just want to convert one particular row to uppercase, use the code below:
df[[1]] <- toupper(df[[1]])
'Program Tip' 카테고리의 다른 글
ANSI 색상 코드를 존중하는 Eclipse 콘솔보기? (0) | 2020.10.29 |
---|---|
Twitter 부트 스트랩을 사용한 툴팁 (0) | 2020.10.29 |
SQL LEFT JOIN 하위 쿼리 별칭 (0) | 2020.10.29 |
선택한 열에 대한 테이블의 NA 값을 바꾸는 방법 (0) | 2020.10.29 |
Angular ng-options에서 값을 연결할 수 있습니까? (0) | 2020.10.29 |