{"action":"closed","issue":{"url":"https://api.github.com/repos/sfirke/janitor/issues/207","repository_url":"https://api.github.com/repos/sfirke/janitor","labels_url":"https://api.github.com/repos/sfirke/janitor/issues/207/labels{/name}","comments_url":"https://api.github.com/repos/sfirke/janitor/issues/207/comments","events_url":"https://api.github.com/repos/sfirke/janitor/issues/207/events","html_url":"https://github.com/sfirke/janitor/issues/207","id":330810192,"node_id":"MDU6SXNzdWUzMzA4MTAxOTI=","number":207,"title":"Feature Request? Elevate a Row to a Header","user":{"login":"billdenney","id":10359931,"node_id":"MDQ6VXNlcjEwMzU5OTMx","avatar_url":"https://avatars1.githubusercontent.com/u/10359931?v=4","gravatar_id":"","url":"https://api.github.com/users/billdenney","html_url":"https://github.com/billdenney","followers_url":"https://api.github.com/users/billdenney/followers","following_url":"https://api.github.com/users/billdenney/following{/other_user}","gists_url":"https://api.github.com/users/billdenney/gists{/gist_id}","starred_url":"https://api.github.com/users/billdenney/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/billdenney/subscriptions","organizations_url":"https://api.github.com/users/billdenney/orgs","repos_url":"https://api.github.com/users/billdenney/repos","events_url":"https://api.github.com/users/billdenney/events{/privacy}","received_events_url":"https://api.github.com/users/billdenney/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":{"login":"billdenney","id":10359931,"node_id":"MDQ6VXNlcjEwMzU5OTMx","avatar_url":"https://avatars1.githubusercontent.com/u/10359931?v=4","gravatar_id":"","url":"https://api.github.com/users/billdenney","html_url":"https://github.com/billdenney","followers_url":"https://api.github.com/users/billdenney/followers","following_url":"https://api.github.com/users/billdenney/following{/other_user}","gists_url":"https://api.github.com/users/billdenney/gists{/gist_id}","starred_url":"https://api.github.com/users/billdenney/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/billdenney/subscriptions","organizations_url":"https://api.github.com/users/billdenney/orgs","repos_url":"https://api.github.com/users/billdenney/repos","events_url":"https://api.github.com/users/billdenney/events{/privacy}","received_events_url":"https://api.github.com/users/billdenney/received_events","type":"User","site_admin":false},"assignees":[{"login":"billdenney","id":10359931,"node_id":"MDQ6VXNlcjEwMzU5OTMx","avatar_url":"https://avatars1.githubusercontent.com/u/10359931?v=4","gravatar_id":"","url":"https://api.github.com/users/billdenney","html_url":"https://github.com/billdenney","followers_url":"https://api.github.com/users/billdenney/followers","following_url":"https://api.github.com/users/billdenney/following{/other_user}","gists_url":"https://api.github.com/users/billdenney/gists{/gist_id}","starred_url":"https://api.github.com/users/billdenney/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/billdenney/subscriptions","organizations_url":"https://api.github.com/users/billdenney/orgs","repos_url":"https://api.github.com/users/billdenney/repos","events_url":"https://api.github.com/users/billdenney/events{/privacy}","received_events_url":"https://api.github.com/users/billdenney/received_events","type":"User","site_admin":false}],"milestone":null,"comments":6,"created_at":"2018-06-08T21:37:24Z","updated_at":"2018-06-22T04:02:45Z","closed_at":"2018-06-22T04:02:45Z","author_association":"NONE","body":"As described in my last issue (#206), I often work with messy data.  One common refrain is that upon load, I sometimes need to change which row is the header.\r\n\r\n```r\r\n#' Elevate a row to be the header of a data.frame\r\n#'\r\n#' @param dat The input data.frame\r\n#' @param row_number The row of \\code{dat} to make into the header\r\n#' @param remove_row Remove \\code{row_number} from the return value?\r\n#' @param remove_rows_above If \\code{row_number != 1}, remove rows\r\n#'   \\code{1:(row_number-1)}?\r\n#' @param force_unique Ensure that the column names are unique?\r\n#' @return A data frame with new names and a cleaned-up header\r\n#' @example\r\n#' data.frame(X__1=c(NA, \"Title\", 1:3),\r\n#'            X__2=c(NA, \"Title2\", 4:6),\r\n#'            stringsAsFactors=FALSE) %>%\r\n#'   row_to_header(row_number=2)\r\n#' @export\r\nrow_to_header <- function(dat, row_number=1, remove_row=TRUE, remove_rows_above=TRUE, force_unique=TRUE) {\r\n  # Check inputs\r\n  if (length(row_number) != 1) {\r\n    stop(\"row_number must be a scalar\")\r\n  }\r\n  new_names <- as.character(unlist(dat[row_number,], use.names=FALSE))\r\n  if (force_unique) {\r\n    new_names <- make.unique(new_names, sep=\"_\")\r\n  } else if (any(duplicated(new_names))) {\r\n    warning(\"Row \", row_number, \" does not provide unique names (consider the force_unique argument)\")\r\n  }\r\n  names(dat) <- new_names\r\n  rows_to_remove <- c(\r\n    if(remove_row) {\r\n      row_number\r\n    } else {\r\n      c()\r\n    },\r\n    if (remove_rows_above) {\r\n      seq_len(row_number-1)\r\n    } else {\r\n      c()\r\n    })\r\n  if (length(rows_to_remove)) {\r\n    dat[-(rows_to_remove),,drop=FALSE]\r\n  } else {\r\n    dat\r\n  }\r\n}\r\n\r\ndata.frame(X__1=c(NA, \"Title\", 1:3),\r\n           X__2=c(NA, \"Title2\", 4:6),\r\n           stringsAsFactors=FALSE) %>%\r\n  row_to_header(row_number=1)\r\ndata.frame(X__1=c(NA, \"Title\", 1:3),\r\n           X__2=c(NA, \"Title2\", 4:6),\r\n           stringsAsFactors=FALSE) %>%\r\n  row_to_header(row_number=2)\r\n```\r\n"}}
{"actor":{"display_login":"sfirke"}}