关于 r:使用 dplyr 我们可以仅将数据类型为整数的列更改为数字数据类型吗

using dplyr can we change to numeric data type only those columns for which data type is integer

我想知道是否有任何方法可以使用 dplyr 或基本包仅将整数数据类型的列转换为数字?

我的数据集是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
structure(list(V1 = c("AA0101","AA0101","AA0102","AA0102","AA0103","AA0103"),
               V2 = 38080:38085,
               V3 = c(0L, 50353564L, 13000567L, 50395060L, 0L, 0L),
               V4 = c(0L, 2L, 2L, 1L, 0L, 0L),
               V5 = c("PS","NW","PS","NW","PS","NW"),
               V6 = c("4/1/2019","4/1/2019","4/1/2019","4/1/2019","4/1/2019","4/1/2019"),
               V7 = c("20:06:04","20:22:17","20:41:53","21:31:04","21:58:51","23:08:04"),
               V8 = c("20:06:14","20:22:22","20:41:58","21:31:11","21:59:01","23:08:10"),
               V9 = c(10L, 5L, 5L, 7L, 10L, 6L),
               V10 = c("0:00:00","20:22:22","20:41:58","21:31:11","0:00:00","0:00:00"),
               V11 = c("0:00:00","20:23:58","20:42:55","21:31:31","0:00:00","0:00:00"),
               V12 = c(0L, 96L, 57L, 20L, 0L, 0L),
               V13 = c("AGENT","AGENT","AGENT","AGENT","AGENT","HANG"),
               V14 = c("20:06:13","20:23:57","20:42:54","21:31:30","21:59:00","0:00:00"),
               V15 = c("20:08:07","20:25:41","20:43:43","21:34:52","22:01:03","0:00:00"),
               V16 = c(114L, 104L, 49L, 202L, 123L, 0L),
               V17 = c("DORIT","SHLOMO","DORIT","ZOHARI","DORIT","NO_SERVER")))

当我这样做时:

1
 str(dat)

它返回给我以下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Classes ‘data.table’ and 'data.frame':  6 obs. of  17 variables:
 $ V1 : chr "AA0101""AA0101""AA0102""AA0102" ...
 $ V2 : int  38080 38081 38082 38083 38084 38085
 $ V3 : int  0 50353564 13000567 50395060 0 0
 $ V4 : int  0 2 2 1 0 0
 $ V5 : chr "PS""NW""PS""NW" ...
 $ V6 : chr "4/1/2019""4/1/2019""4/1/2019""4/1/2019" ...
 $ V7 : chr "20:06:04""20:22:17""20:41:53""21:31:04" ...
 $ V8 : chr "20:06:14""20:22:22""20:41:58""21:31:11" ...
 $ V9 : int  10 5 5 7 10 6
 $ V10: chr "0:00:00""20:22:22""20:41:58""21:31:11" ...
 $ V11: chr "0:00:00""20:23:58""20:42:55""21:31:31" ...
 $ V12: int  0 96 57 20 0 0
 $ V13: chr "AGENT""AGENT""AGENT""AGENT" ...
 $ V14: chr "20:06:13""20:23:57""20:42:54""21:31:30" ...
 $ V15: chr "20:08:07""20:25:41""20:43:43""21:34:52" ...
 $ V16: int  114 104 49 202 123 0
 $ V17: chr "DORIT""SHLOMO""DORIT""ZOHARI" ...
 - attr(*,".internal.selfref")=<externalptr>


使用 base R 我们可以做到

1
2
i1 <- sapply(dat, is.integer)
dat[i1] <- lapply(dat[i1], as.numeric)