我有一个包含库存数据的数据框:
stock,value
stock1,$30
stock2,£31
stock3,$50
如何将列值溢出以使两列具有货币和值。 输出的一个例子:
stock,currency,value
stock1,$,30
stock2,£,31
stock3,$,50
答案 0 :(得分:4)
尝试
library(data.table)#v1.9.5+
setDT(df1)[, c('currency', 'value') :=tstrsplit(value,
'(?<=[^0-9])', perl=TRUE, type.convert=TRUE)]
df1
# stock value currency
#1: stock1 30 $
#2: stock2 31 £
#3: stock3 50 $
或者
library(tidyr)
extract(df1, value, into=c('currency', 'value'), '([^0-9]+)(.*)')
# stock currency value
#1 stock1 $ 30
#2 stock2 £ 31
#3 stock3 $ 50
或使用base R
cbind(df1[1],read.table(text=sub('(?<=[^0-9])', ' ', df1$value,
perl=TRUE), col.names=c('currency', 'value')))
# stock currency value
#1 stock1 $ 30
#2 stock2 £ 31
#3 stock3 $ 50
df1 <- structure(list(stock = c("stock1", "stock2", "stock3"),
value = c("$30",
"£31", "$50")), .Names = c("stock", "value"), class = "data.frame",
row.names = c(NA, -3L))
答案 1 :(得分:2)
Akrun打败了我,但这又是另一个:
require(stringr)
str_match(c('$30', '£500'), '^([$£])([0-9\\.]+)' )[,2:3]
[,1] [,2]
[1,] "$" "30"
[2,] "£" "500"
你可以充实其余部分。
答案 2 :(得分:0)
使用sqldf
:
sqldf("SELECT stock,
SUBSTR(value, 1, 1) As currency,
SUBSTR(value, 2, LENGTH(value)) AS value
FROM df1")
或
sqldf("SELECT stock,
rtrim(value,'0123456789') AS currency,
ltrim(value,'$£') AS value
FROM df1")
输出:
stock currency value
1 stock1 $ 30
2 stock2 £ 31
3 stock3 $ 50