Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.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
删除列(R)中字符串的一部分_R - Fatal编程技术网

删除列(R)中字符串的一部分

删除列(R)中字符串的一部分,r,R,我有数据帧dataf和列PlayerValue PlayerName playerValue 1 Michy Batshuayi 40,00 Mill. € 2 Tiemoué Bakayoko 35,00 Mill. € 3 Kurt Zouma 20,00 Mill. € 4 Kenedy 10,00 Mill. € 5

我有数据帧
dataf
和列
PlayerValue

         PlayerName           playerValue
1     Michy Batshuayi        40,00 Mill. €  
2     Tiemoué Bakayoko       35,00 Mill. €  
3     Kurt Zouma             20,00 Mill. €  
4     Kenedy                 10,00 Mill. €  
5     Tammy Abraham          10,00 Mill. €  
6     Abdul Rahman Baba      8,00 Mill. €  
7     Mario Pasalic          8,00 Mill. €  
8     Lewis Baker            5,50 Mill. €  
9     Ola Aina               4,00 Mill. €  
10    Tomas Kalas            4,00 Mill. €  
我想让它只得到像这样的列中的数字(如果可能的话,用小数点替换逗号)

         PlayerName           playerValue
1     Michy Batshuayi           40,00 # 40.00, if possible
2     Tiemoué Bakayoko          35,00  
3     Kurt Zouma                20,00  
4     Kenedy                    10,00  
5     Tammy Abraham             10,00   
6     Abdul Rahman Baba         8,00   
7     Mario Pasalic             8,00  
8     Lewis Baker               5,50  
9     Ola Aina                  4,00   
10    Tomas Kalas               4,00   

使用
gsub
替换空格后的任何内容,并将
替换为
,如下所示:

data$playerValue <- gsub(",", ".", gsub("[[:space:]].*", "", data$playerValue))
这样,如果要将其转换为数字,可以按如下操作:

data$playerValue <- as.numeric(data$playerValue)

data$playerValue使用
gsub
替换空格后的任何内容,并将
替换为
,如下所示:

data$playerValue <- gsub(",", ".", gsub("[[:space:]].*", "", data$playerValue))
这样,如果要将其转换为数字,可以按如下操作:

data$playerValue <- as.numeric(data$playerValue)

data$playerValue这样就可以了

playerValue <- "40,00 Mill. € "
as.numeric(gsub("^(\\d+?)\\,(\\d+?)\\s.*", "\\1.\\2", playerValue, perl = TRUE))
# returns
40

playerValue这样就可以了

playerValue <- "40,00 Mill. € "
as.numeric(gsub("^(\\d+?)\\,(\\d+?)\\s.*", "\\1.\\2", playerValue, perl = TRUE))
# returns
40

playerValue如果列始终为
“#########Mill.€”
格式,则只需将非数字部分替换为空白字符
。如
库(stringr)中所示;x如果列始终为
“#########Mill.€”
格式,则只需将非数字部分替换为空白字符
。如
库(stringr)中所示;x