R의 열 수 얻기
Sheeraz Gul
2023년6월21일
R
R Data Frame
![R의 열 수 얻기](/img/R/feature-image---column-number-in-r.webp)
R에는 총 열 수와 이름별로 열 수를 가져오는 내장 함수가 있습니다. 이 튜토리얼은 R에서 열 수를 얻는 방법을 보여줍니다.
R의 총 열 수 얻기
기본 제공 함수 ncol()
은 R 언어의 총 열 수를 가져오는 데 사용됩니다. 데이터 프레임이라는 하나의 매개변수를 사용합니다.
예제 코드:
#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
Id=c(101, 102, 103, 104, 105),
Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))
#find number of columns
number_columns <- ncol(Delftstack)
#print the number of columns
cat("Number of columns in the Data Frame is:", number_columns)
위의 코드는 데이터 프레임의 총 열 수를 확인합니다.
출력:
Number of columns in the Data Frame is: 4
R에서 이름으로 열 수 얻기
colnames(df)
메소드와 함께 which()
메소드를 사용하여 이름으로 열을 가져올 수도 있습니다.
예제 코드:
#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
Id=c(101, 102, 103, 104, 105),
Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))
# Get the column number by name using Which method
which( colnames(Delftstack)=="Id" )
위의 코드는 열 ID의 수가 3
이므로 열 이름으로 열 번호를 가져옵니다.
출력:
[1] 3
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook