如何将一串矩阵转换成一个字符串? [英] How can I convert a matrix of strings into a tibble?

查看:164
本文介绍了如何将一串矩阵转换成一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.rData文件中给出了一些数据。格式是字符模式下的 xts 对象。 (我意识到这是一种不寻常的格式,但我无法控制)

 >头(行)
SYMBOL EX价格大小COND BID BIDSIZ OFR
2012-05-04 09:30:00BACT7.8938538F7.89523 7.9
2012-05-04 09:30:01BACZ7.885288@7.88610337.9
2012-05- 04 09:30:03BACX7.891000@7.8819747.89
2012-05-04 09:30:07BACT 7.8919052F7.8810587.89
2012-05-04 09:30:08BACY7.8985053F7.88 1081017.9
2012-05-04 09:30:09BACD7.890110219@7.892687.9

>模式(交易)
'字符'

我想通过转换成一个更加细腻的格式,即一个琐事,所以我可以将列存储为数据时间,双精度和整数。



我已经设法使用以下代码实现: / p>

 > trades_ = bind_cols(data_frame(DATE = index(trades)),as_data_frame(coredata(trades)))%>%
mutate_at(as.numeric,.cols = vars(PRICE,BID,OFR))%> ;%
mutate_at(as.integer,.cols = vars(SIZE,BIDSIZ,OFRSIZ))

>头(trades_)
#一个琐事:6×10
DATE SYMBOL EX PRICE SIZE COND BID BIDSIZ OFR
< dttm> < CHR> < CHR> < DBL> < INT> < CHR> < DBL> < INT> < DBL>
1 2012-05-04 09:30:00 BAC T 7.8900 38538 F 7.89 523 7.90
2 2012-05-04 09:30:01 BAC Z 7.8850 288 @ 7.88 61033 7.90
3 2012-05-04 09:30:03 BAC X 7.8900 1000 @ 7.88 1974 7.89
4 2012-05-04 09:30:07 BAC T 7.8900 19052 F 7.88 1058 7.89
5 2012-05 -04 09:30:08 BAC Y 7.8900 85053 F 7.88 108101 7.90
6 2012-05-04 09:30:09 BAC D 7.8901 10219 @ 7.89 268 7.90
/ pre>

我想知道是否已经有了一个内置的功能。查看的每列的东西,交易矩阵,并确定它是一列整数,双精度等,并将其转换为适当的类型。



这是一个csv解析器将要做的事情。

解决方案

从权威的答案,但我最终这样做:

  smarter_type_convert = function(vector){
converted_vector = type .convert(vector)
if(is.numeric(converted_vector)){
int_vector = as.integer(converted_vector)
if(isTRUE(all.equal(int_vector,converted_vector,check.attributes = FALSE))){
int_vector
} else {
converted_vector
}
} else {
converted_vector
}
}

trades%>%coredata%>%as_data_frame%>%mutate_all(smarter_type_convert)


I'm given some data in an .rData file. The format is an xts object in character mode. (I realise this is an unusual format, but I have no control over it)

> head(trades)
                    SYMBOL EX  PRICE    SIZE    COND BID    BIDSIZ   OFR   
2012-05-04 09:30:00 "BAC"  "T" "7.89"   "38538" "F"  "7.89" "523"    "7.9" 
2012-05-04 09:30:01 "BAC"  "Z" "7.885"  "288"   "@"  "7.88" "61033"  "7.9" 
2012-05-04 09:30:03 "BAC"  "X" "7.89"   "1000"  "@"  "7.88" "1974"   "7.89"
2012-05-04 09:30:07 "BAC"  "T" "7.89"   "19052" "F"  "7.88" "1058"   "7.89"
2012-05-04 09:30:08 "BAC"  "Y" "7.89"   "85053" "F"  "7.88" "108101" "7.9" 
2012-05-04 09:30:09 "BAC"  "D" "7.8901" "10219" "@"  "7.89" "268"    "7.9" 

> mode(trades)
'character'

I would like to process this data by converting into a saner format, namely a tibble, so that I can store the columns as datetimes, doubles and integers.

I've managed to achieve this with the following code:

> trades_ =  bind_cols(data_frame(DATE=index(trades)), as_data_frame(coredata(trades))) %>%
    mutate_at(as.numeric, .cols=vars(PRICE, BID, OFR)) %>%
    mutate_at(as.integer, .cols=vars(SIZE, BIDSIZ, OFRSIZ))

> head(trades_)
# A tibble: 6 × 10
                 DATE SYMBOL    EX  PRICE  SIZE  COND   BID BIDSIZ   OFR
               <dttm>  <chr> <chr>  <dbl> <int> <chr> <dbl>  <int> <dbl>
1 2012-05-04 09:30:00    BAC     T 7.8900 38538     F  7.89    523  7.90   
2 2012-05-04 09:30:01    BAC     Z 7.8850   288     @  7.88  61033  7.90
3 2012-05-04 09:30:03    BAC     X 7.8900  1000     @  7.88   1974  7.89   
4 2012-05-04 09:30:07    BAC     T 7.8900 19052     F  7.88   1058  7.89   
5 2012-05-04 09:30:08    BAC     Y 7.8900 85053     F  7.88 108101  7.90
6 2012-05-04 09:30:09    BAC     D 7.8901 10219     @  7.89    268  7.90

I'm wondering if there's already a built-in function for this. Something that looks at each column of the trades matrix and figures out whether it's a column of integers, doubles, etc and converts it to the appropriate type.

This is the sort of thing that a csv parser would do.

解决方案

This is far from an authoritative answer, but I ended up doing this:

smarter_type_convert = function (vector) {
    converted_vector = type.convert(vector)
    if (is.numeric(converted_vector)) {
        int_vector = as.integer(converted_vector)
        if (isTRUE(all.equal(int_vector, converted_vector, check.attributes=FALSE))) {
            int_vector
        } else {
            converted_vector
        }
    } else {
        converted_vector
    }
}

trades %>% coredata %>% as_data_frame %>% mutate_all(smarter_type_convert)

这篇关于如何将一串矩阵转换成一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆