在 R 中将高度从英尺 (6-1) 转换为英寸 (73) [英] Convert Height from Ft (6-1) to Inches (73) in R

查看:36
本文介绍了在 R 中将高度从英尺 (6-1) 转换为英寸 (73)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一列英尺转换为英寸.通常它是简单的乘法,但是当格式分别为 (5-10) 或 (6-2) 时,分别为 70 和 74 英寸时,我对如何转换感到困惑.到目前为止,这是我的代码.我正在尝试更改 combine.data$Ht

I am trying to convert a column of feet to inches. Usually it is simple multiplication, but I am confused on how to convert when the format is (5-10) or (6-2) for 70 and 74 inches respectively. Here is my code so far. I am trying to change combine.data$Ht

library(rvest)
library(magrittr)
library(dplyr)
library(purrr)

years <- 2010:2020

urls <- paste0(
  'https://www.pro-football-reference.com/draft/',
  years,
  '-combine.htm')

combine.data <- map(
  urls,
  ~read_html(.x) %>% 
    html_nodes(".stats_table") %>% 
    html_table() %>% 
    as.data.frame()
) %>%
  set_names(years) %>% 
  bind_rows(.id = "year") %>% 
  filter(Pos == 'CB' | Pos == "S")

推荐答案

您可以将 Ht 列拆分为两个单独的列 feetinches 然后执行计算以计算以英尺为单位的高度.

You can split the Ht column into two separate columns feet and inches and then perform the calculation to calculate height in feet.

library(dplyr)
library(tidyr)

combine.data %>%
  separate(Ht,c('feet', 'inches'), sep = '-', convert = TRUE, remove = FALSE) %>%
  mutate(feet = 12*feet + inches) %>%
  select(-inches)

这篇关于在 R 中将高度从英尺 (6-1) 转换为英寸 (73)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆