我想在列中用逗号分隔数字 用其他数字。 这是我的输入
> df = data.frame (SAMPLE1.DP=c("555","651","641","717"), SAMPLE1.AD=c("555", "68,583","2,639","358,359"), SAMPLE2.DP=c("1023","930","683","1179"), SAMPLE2.AD=c("1023","0,930","683","585,594")) > df SAMPLE1.DP SAMPLE1.AD SAMPLE2.DP SAMPLE2.AD 1 555 555 1023 1023 2 651 68,583 930 0,930 3 641 2,639 683 683 4 717 358,359 1179 585,594
最后,我想添加两列(AD / DP),分别将值SAMPLE1.AD除以SAMPLE1.DP,将值SAMPLE2.AD除以SAMPLE2.DP,它们分别表示逗号两侧的数字的百分比,像这样:
> end = data.frame(SAMPLE1.DP=c("555","651","641","717"), + SAMPLE1.AD=c("555", "68,583","204,437","358,359"), + SAMPLE1.AD_DP=c("1.00","0.10,0.90","0.32,0.68","0.50,0.50"), + SAMPLE2.DP=c("1023","930","683","1179"), + SAMPLE2.AD=c("1023","0,930","683","585,594"), + SAMPLE2.AD_DP=c("1.00","0.00,1.00","1.00","0.49,0,51")) >end SAMPLE1.DP SAMPLE1.AD SAMPLE1.AD_DP SAMPLE2.DP SAMPLE2.AD SAMPLE2.AD_DP 1 555 555 1.00 1023 1023 1.00 2 651 68,583 0.10,0.90 930 0,930 0.00,1.00 3 641 204,437 0.32,0.68 683 683 1.00 4 717 358,359 0.50,0.50 1179 585,594 0.49,0,51
表示: XX YY,ZZ YY / XX,ZZ / XX AA BB,CC BB / AA,CC / AA
如果我将表中的值视为数字,则无法使用,因为值之间用逗号分隔...
您有任何想法吗?
预先感谢您的帮助
答案 0 :(得分:1)
您需要做的第一件事是将 <typegroup name="LikeReview">
<itemtype code="LikeReview" autocreate="true" generate="true">
<deployment table="likeReview" typecode="15088"/>
<attributes>
<attribute type="Customer" qualifier="customer" >
<modifiers optional="false" unique="true"/>
<persistence type="property"/>
</attribute>
<attribute type="CustomerReview" qualifier="review" >
<modifiers optional="false" unique="true"/>
<persistence type="property"/>
</attribute>
<attribute qualifier="isLike" type="java.lang.Boolean">
<defaultvalue>Boolean.FALSE</defaultvalue>
<persistence type="property"/>
</attribute>
</attributes>
</itemtype>
</typegroup>
替换为,
并转换为数字。然后根据您所需的条件进行拆分并进行划分,即
.
答案 1 :(得分:1)
如果您的数字中有逗号,则该列很可能已中毒并被强制转换为字符。您需要做的是将列转换为数字,然后分别划分每一列。
library(tidyverse)
dat <- tribble(~"SAMPLE1.DP", ~"SAMPLE1.AD", ~"SAMPLE2.DP", ~"SAMPLE2.AD",
555, 555, 1023, 1023,
651, "2,647", 930, ",93",
641, "2,639", 683, 683,
717, "358,359", 1179, "585,594")
dat %>%
mutate_at(c(2,4), list(~str_replace(., ",", "."))) %>%
mutate_all(as.numeric) %>%
mutate(addp1 = SAMPLE1.AD / SAMPLE1.DP,
addp2 = SAMPLE2.AD / SAMPLE2.DP)
#> # A tibble: 4 x 6
#> SAMPLE1.DP SAMPLE1.AD SAMPLE2.DP SAMPLE2.AD addp1 addp2
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 555 555 1023 1023 1 1
#> 2 651 2.65 930 0.93 0.00407 0.001
#> 3 641 2.64 683 683 0.00412 1
#> 4 717 358. 1179 586. 0.500 0.497
由reprex package(v0.2.1)于2019-05-20创建
答案 2 :(得分:0)
谢谢大家,但我的问题不太清楚,很抱歉。
在我的输入示例中,我只用逗号分隔整数,没有小数点。
例如,在我的示例的第3行: 2,647表示2 AND 647,我想将两个数字均除以651,以便得到结果:2/651,647/651,所以它将是0.01和0.99(或1%和99%)
它们是整数(或整数),用逗号分隔。
希望我更清楚...谢谢...