"R 데이터프레임 컬럼명으로 컬럼 제거"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 18개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;R 데이터프레임 컬럼명으로 컬럼 제거
;R 데이터프레임 컬럼명으로 컬럼 제거


==방법 1: subset()==
<syntaxhighlight lang='r' run>
<syntaxhighlight lang='r' run>
df = read.table( header=TRUE, stringsAsFactors=FALSE, text="
df <- iris
Class Name  English Math
head(df)
A    Alice 90      60
</syntaxhighlight>
A      Bob 80      NA
<syntaxhighlight lang='r' run>
B    Carol NA      80
df <- iris
B      Dave 60      90
df2 <- subset(df, select=-Petal.Length)
")
head(df2)
options(echo=T)
</syntaxhighlight>
df[!(names(df) %in% c("Name","Math"))]
<syntaxhighlight lang='r' run>
df[,!(names(df) %in% c("Name","Math"))]
df <- iris
df2 <- subset(df, select=-c(Petal.Length))
head(df2)
</syntaxhighlight>
<syntaxhighlight lang='r' run>
df <- iris
df2 <- subset(df, select=-c(Petal.Length, Petal.Width))
head(df2)
</syntaxhighlight>
 
==방법 2: names()==
<syntaxhighlight lang='r' run>
df <- iris
head(df)
</syntaxhighlight>
<syntaxhighlight lang='r' run>
df <- iris
df2 <- df[!(names(df) %in% c("Petal.Length", "Petal.Width"))]
head(df2)
</syntaxhighlight>
<syntaxhighlight lang='r' run>
df <- iris
df2 <- df[,!(names(df) %in% c("Petal.Length", "Petal.Width"))]
head(df2)
</syntaxhighlight>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[R %in%]]
* [[R names()]]
* [[R subset()]]
* [[R 데이터프레임 컬럼 제거]]
* [[R 데이터프레임 컬럼 제거]]
* [[R 데이터프레임 n번째 컬럼 제거]]
* [[R 데이터프레임 n번째 컬럼 제거]]
* [[Pandas 컬럼명으로 컬럼 제거]]


[[분류: R 데이터프레임]]
[[분류: R 데이터프레임]]

2021년 10월 27일 (수) 00:24 기준 최신판

1 개요[ | ]

R 데이터프레임 컬럼명으로 컬럼 제거

2 방법 1: subset()[ | ]

df <- iris
head(df)
df <- iris
df2 <- subset(df, select=-Petal.Length)
head(df2)
df <- iris
df2 <- subset(df, select=-c(Petal.Length))
head(df2)
df <- iris
df2 <- subset(df, select=-c(Petal.Length, Petal.Width))
head(df2)

3 방법 2: names()[ | ]

df <- iris
head(df)
df <- iris
df2 <- df[!(names(df) %in% c("Petal.Length", "Petal.Width"))]
head(df2)
df <- iris
df2 <- df[,!(names(df) %in% c("Petal.Length", "Petal.Width"))]
head(df2)

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}