Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据帧中的三个变量从chr转换为R中的int_R_Integer_Character - Fatal编程技术网

将数据帧中的三个变量从chr转换为R中的int

将数据帧中的三个变量从chr转换为R中的int,r,integer,character,R,Integer,Character,对R来说很新,但我希望这会很简单。我有一个html表,我把它刮到了R中,它看起来像这样: `'data.frame': 238 obs. of 6 variables: $ Facility Name : chr "Affinity Healthcare Center" "Alameda Care Center" "Alcott Rehabilitation Hospital" "Alden Terrace Convalescent Hospital" ... $ Cit

对R来说很新,但我希望这会很简单。我有一个html表,我把它刮到了R中,它看起来像这样:

`'data.frame':  238 obs. of  6 variables:
$ Facility Name      : chr  "Affinity Healthcare Center"     "Alameda Care Center" "Alcott Rehabilitation Hospital" "Alden Terrace Convalescent Hospital" ...
$ City               : chr  "Paramount" "Burbank" "Los Angeles" "Los Angeles" ...
$ State              : chr  " CA" " CA" " CA" " CA" ...
$ Confirmed Staff    : chr  "26" "36" "14" "27" ...
$ Confirmed Residents: chr  "29" "49" "26" "85" ...
$ Total Deaths       : chr  26 36 14 27 19 3 1 7 16 3 ...`
我希望
确认的员工
确认的居民
总死亡人数
都是整数,这样我就可以对他们做一些数学运算,排序,排序等等

我尝试了一个变量,它似乎工作正常:

`tbls_ls4$`Total Deaths` <- as.integer(tbls_ls4$`Confirmed Staff`)`

有几种方法可以做到这一点:

库(data.table)
setDT(df)

cols您还可以使用
tidyverse
包中的
mutate_at
将某些列从字符转换为整数

library(tidyverse)

df<-df %>%
  # Indicate the column names you wish to convert inside vars
  mutate_at(vars(ConfirmedStaff,ConfirmedResidents,TotalDeaths),
            # Indicate the function to apply to each column
            as.integer)
库(tidyverse)
df%
#指示要在变量内转换的列名
在(VAR)处发生突变(确认的斯塔夫、确认的住户、总死亡人数),
#指示要应用于每列的函数
as.整数)

因素不是更好吗?这能回答你的问题吗
lappy(df,as.integer)
dplyr::mutate(跨越(is.character,~as.integer)
请注意,
只有在
dplyr
@NelsonGon的开发版本中才能使用
变量。我问@jkandel,他/她是否知道
因子
变量允许排序/排序等,而不会丢失关于标签的信息。我认为,考虑到这个问题,
因子
更适合前两个变量流氓