通过索引更改数据类型

时间:2019-07-30 20:39:40

标签: r tidyverse

我想将某些列更改为整数(从表的4列更改为最后一个列)。我该怎么办?

My data

我已经尝试过

databianca %>% 
  mutate_if(is.factor, ~as.integer(as.character(.)))

databianca %>% 
  mutate_at(vars(columbia:escrita.POS), ~as.integer(as.character(.)))

同时收到这两个警告:

警告消息: 1:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

2:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

3:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

4:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

5:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

6:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

7:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

8:在(函数(...,.x = ..1,.y = ..2,。= ..1)中:   强制引入的NAs

难道没有选择将从哥伦比亚到最后一列的所有列(在Matlab中我将使用4:end给出从第4列到最后一个元素的指令)作为整数吗?

我通过dpasta收集的数据:

databianca <- data.frame(
SEXO = as.factor(c("F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M")),
GRUPOS = as.factor(c("C", "E", "E", "C", "C", "C", "E", "E", "E",
"C", "E", "E", "E", "C", "C", "C", "E", "C",
"E", "E", "C", "E", "C", "C", "C", "E", "E",
"E", "C", "E", "C", "C", "C", "E", "C", "E", "C",
"E", "E", "C", "C", "C", "E", "E", "E", "E",
"C", "C", "C", "C")),
SALAS = as.factor(c("B", "A", "C", "A", "A", "C", "A", "B", "C",
"C", "C", "B", "A", "C", "A", "C", "A", "C",
"C", "C", "B", "B", "B", "A", "B", "C", "A",
"B", "A", "A", "C", "B", "B", "A", "A", "B", "B",
"B", "C", "B", "B", "B", "B", "A", "A", "B",
"C", "C", "A", "B")),
token = c(100L, 95L, 106L, 112L, 84L, 125L, 108L, 114L, 112L,
117L, 84L, 96L, 124L, 118L, 99L, 84L, 95L, 113L, 123L,
135L, 100L, 92L, 101L, 129L, 93L, 106L, 101L, 84L, 84L,
128L, 113L, 95L, 109L, 127L, 112L, 108L, 108L, 118L, 97L,
112L, 116L, 88L, 92L, 111L, 123L, 112L, 116L, 104L, 90L,
89L),
hab.visuo = c(3L, 3L, 2L, 0L, 0L, 1L, 3L, 3L, 2L, 1L, 1L, 1L, 0L, 2L,
1L, 1L, 2L, 2L, 0L, 7L, 3L, 2L, 0L, 2L, 0L, 3L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 2L, 2L, 1L, 1L, 0L, 0L, 0L, 1L, 2L,
1L, 1L, 0L, 2L, 0L, 0L, 3L, 3L),
tamanho = c(7L, 4L, 8L, 7L, 7L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 6L, 7L, 7L, 8L, 8L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 7L,
7L, 7L, 8L, 8L, 7L, 8L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 7L, 8L, 8L, 7L, 7L),
forma = c(6L, 6L, 7L, 4L, 6L, 6L, 6L, 7L, 6L, 6L, 6L, 6L, 6L, 5L,
6L, 7L, 5L, 6L, 7L, 7L, 6L, 7L, 0L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 6L, 7L, 7L, 6L, 7L, 7L,
6L, 7L, 6L, 6L, 6L, 6L, 6L, 7L),
nomeou.cor = c(67L, 82L, 85L, 83L, 68L, 72L, 74L, 74L, 80L, 80L, 63L,
70L, 80L, 81L, 68L, 72L, 81L, 76L, 78L, 81L, 81L, 80L,
65L, 78L, 74L, 75L, 61L, 63L, 69L, 79L, 70L, 75L, 77L,
79L, 74L, 78L, 70L, 79L, 71L, 70L, 78L, 53L, 67L, 75L, 83L,
82L, 84L, 71L, 53L, 69L),
nomeou.incor = c(23L, 8L, 5L, 7L, 22L, 18L, 16L, 16L, 10L, 10L, 27L,
20L, 10L, 9L, 22L, 18L, 9L, 14L, 12L, 9L, 8L, 10L, 25L,
12L, 16L, 15L, 29L, 27L, 21L, 11L, 20L, 15L, 13L, 11L,
16L, 12L, 20L, 11L, 19L, 20L, 12L, 37L, 23L, 15L, 15L, 8L,
6L, 19L, 38L, 21L),
token.POS = c(120L, 120L, 136L, 117L, 122L, 118L, 135L, 122L, 124L,
112L, 98L, 127L, 130L, 119L, 116L, 94L, 122L, 125L,
130L, 123L, 113L, 116L, 105L, 109L, 113L, 120L, 99L, 87L,
0L, 123L, 0L, 94L, 136L, 125L, 118L, 124L, 123L, 128L,
101L, 124L, 131L, 87L, 116L, 120L, 117L, 119L, 123L, 127L,
91L, 105L),
hab.visuo.POS = c(1L, 2L, 2L, 1L, 2L, 5L, 2L, 1L, 3L, 0L, 2L, 2L, 1L, 2L,
5L, 1L, 0L, 0L, 0L, 5L, 0L, 2L, 0L, 1L, 0L, 2L, 3L, 1L,
0L, 1L, 0L, 1L, 0L, 3L, 1L, 4L, 1L, 0L, 2L, 0L, 1L, 0L,
0L, 2L, 3L, 2L, 7L, 1L, 2L, 3L),
tamanho.POS = c(8L, 8L, 8L, 7L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 8L, 8L, 8L,
8L, 7L, 8L, 8L, 8L, 7L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
0L, 7L, 0L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 7L, 8L, 7L, 7L,
8L, 6L, 8L, 7L, 8L, 7L, 8L, 7L),
forma.POS = c(6L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 6L, 5L, 7L, 6L, 6L, 7L,
5L, 5L, 7L, 5L, 7L, 8L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 6L,
0L, 7L, 0L, 5L, 7L, 7L, 7L, 7L, 2L, 7L, 5L, 5L, 5L, 7L,
6L, 7L, 7L, 6L, 6L, 7L, 2L, 6L),
nomeou.cor.POS = c(78L, 78L, 85L, 73L, 70L, 76L, 77L, 78L, 82L, 74L, 56L,
70L, 79L, 78L, 65L, 67L, 87L, 78L, 82L, 82L, 79L, 84L,
74L, 78L, 71L, 79L, 61L, 70L, 0L, 75L, 0L, 74L, 83L, 77L,
76L, 77L, 75L, 77L, 68L, 79L, 79L, 61L, 67L, 76L, 79L,
78L, 81L, 68L, 53L, 71L),
nomeou.incor.POS = c(12L, 12L, 5L, 17L, 20L, 14L, 13L, 12L, 8L, 16L, 34L,
20L, 11L, 12L, 25L, 23L, 3L, 12L, 8L, 8L, 11L, 6L, 16L,
12L, 19L, 11L, 29L, 20L, 0L, 15L, 0L, 16L, 7L, 13L, 14L,
13L, 15L, 13L, 22L, 11L, 11L, 29L, 23L, 14L, 11L, 12L,
9L, 22L, 37L, 19L),
columbia = as.factor(c("49,3", "55,38", "67,69", "53,33", "38,46",
"53,84", "63,07", "61,53", "63,07", "35,38",
"43,33", "38,33", "55,38", "67,69", "47,69",
"29,23", "60", "53,84", "61,66", "73,33", "47,
69", "46,66", "41,66", "56,66", "47,69", "61,66",
"33,33", "40", "60", "36,66", "51,66", "43,33",
"58,46", "60", "56,92", "38,33", "40", "60",
"53,33", "56,92", "61,53", "53,84", "44,61",
"47,69", "44,61", "66,15", "56,66", "58,33", "65",
"51,66")),
escrita = as.factor(c("2", "2", "2", "2", "2", "1,5", "2,25", "2",
"2", "2", "2", "2", "2", "2,75", "2", "2,5",
"1,75", "2", "3", "4,5", "1,75", "1,75", "2",
"2", "2", "2", "1", "1,5", "2", "2", "2", "2,5",
"2", "2", "2", "1,75", "1,75", "2", "2", "2,
25", "2", "2", "1,75", "1", "2", "0", "2,5", "2",
"1,25", "1,5")),
columbia.POS = as.factor(c("53,8", "66,7", "76,7", "31,7", "43,1", "58,
5", "56,7", "55", "70", "35,4", "28,3", "43,
3", "61,7", "68,3", "55,4", "28,3", "55", "56,7",
"68,3", "66,7", "41,5", "56,7", "50", "65",
"58,5", "75", "48,3", "31,7", "0", "53,3", "0",
"50", "65", "66,7", "73,8", "51,7", "45", "66,
7", "48,3", "69,2", "73,8", "43,3", "56,7", "47,
7", "51,7", "73,3", "66,7", "38,3", "43,3", "33,
3")),
escrita.POS = as.factor(c("2", "3", "3", "1,5", "1,5", "2", "3", "3",
"4", "2", "1,5", "3", "2", "4", "1,5", "3",
"2", "3", "4", "4", "1,5", "2", "3", "1,5", "2",
"2", "1,5", "1,5", "0", "1,5", "0", "2", "3",
"1,5", "1,5", "1,5", "1,5", "2", "1,5", "3",
"2", "2", "2", "1,5", "1,5", "1,5", "5", "3", "1,
5", "1,5"))
)

1 个答案:

答案 0 :(得分:0)

as.integer变量上使用non-integer时会发生这种情况。
看来您的数字中包含“,”(例如:51,66),并且换行符也造成了问题,因此请先使用gsub()使因子“干净”,然后再运行as.integer()

databianca[,4:length(names(databianca))] <- apply(databianca[, 4:length(names(databianca))], 2, function(x) as.integer(gsub("[\n,]", "", x)))

这也应该起作用

databianca[,4:length(names(databianca))] <-  as.integer(gsub("[\n,]", "", as.matrix(databianca[,4:length(names(databianca))])))