HOWTO · R
R의 열 수 얻기
이 튜토리얼은 R에서 열 번호를 얻는 방법을 보여줍니다.
이 페이지의 내용
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