YGN_RTWP$key <- ave(YGN_RTWP$Sitename, FUN = seq_along)

 

YGN_RTWP 데이터 프레임에 key 컬럼 추가해서 순서대로 key값 부여한다.

사용할 때가 종종 있는데 검색해서 못 찾음.

for (n in 1:nrow(dataFrame)){
  dataFrame$Category[n] <- sample(-95:-85, 1, replace = T)
}

1:nrow(dataFrame) 을 못해서 한참 헤맸다.

 

  • 1부터 dataFrame의 행 개수만큼 반복.
  • n번째 행에 Category 값은
  • -95부터 -85까지 값 중 1개를 뽑는다.
  • replace = T를 통해, 이미 나온 숫자가 한 번 더 나올 수 있음.

'프로그래밍 > R' 카테고리의 다른 글

[R] 특정 열 맨 앞으로 옮기기  (0) 2021.11.26
[R] 순서대로 Key값 부여하기  (0) 2021.09.12
[R] gsub() 행 내 변수 일괄 변경  (0) 2021.08.13
Repeater$Type <- gsub("Narrow band", "Narrow Band", Repeater$Type)
Repeater$Type <- gsub("Wideband Repeater", "Wide Band", Repeater$Type)
Repeater$Type <- gsub("Narrow Bands", "Narrow Band", Repeater$Type)

Repeater 데이터의 Type 행에서

"Narrow band", "Wideband Repeater", "Narrow Bands" 를 각각

"Narrow Band", "Wide Band", "Narrow Band"로 일괄 변경하기