如何在这个R数据表上输出另一列? [英] How to present another column on this R data.table output?

查看:155
本文介绍了如何在这个R数据表上输出另一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在输出中提出另一列 DF2 $ time_expected DF $ time / DF2 $ time_expected DF [DF $ Experiment ==A,]
显示某些命令的输出的代码

I am trying to present another columns DF2$time_expected and DF$time/DF2$time_expected in the output of DF[DF$Experiment=="A", ]. Code where outputs of some commands shown

library(data.table)
library('dplyr')
ow <- options("warn")

DF <- read.csv(text= 
"Field,time,T,Experiment
Acute,0,0,A
An,9,120,A
En,15.6,2,A
Fo,9.2,2,A
Acute,8.3,1,B
An,7.7,26,B
En,12.9,1,B
Fo,0,0,B
Acute,7.5,1,C
An,7.9,43,C
En,0,0,C
Fo,5.4,1,C
Acute,8.6,2,D
An,7.8,77,D
En,0,0,D
Fo,0,0,D
Acute,0,0,E
An,7.9,60,E
En,14.3,1,E
Fo,0,0,E
Acute,8.3,4,F
An,8.2,326,F
En,14.6,4,F
Fo,7.9,3,F", 
header=TRUE, sep=",")

# http://stackoverflow.com/a/43695774/54964
DF[DF$Experiment=="A", ]
#  Field time   T Experiment
#1 Acute  0.0   0          A
#2    An  9.0 120          A
#3    En 15.6   2          A
#4    Fo  9.2   2          A
# TODO integrate here relative values of DF$time/DF2$time_expected as another column

DF2 <- read.csv(text=
"Field,time_expected
Acute,6
An,6
En,6
Fo,5", 
header=TRUE, sep=",")
#DF2[DF2$Field=="An", ]

## Now compare DF.time to DF2.time_expected by DF.time/DF2.time_expected
DF$time/DF2$time_expected
# [1] 0.000000 1.500000 2.600000 1.840000 1.383333 1.283333 2.150000 0.000000
# [9] 1.250000 1.316667 0.000000 1.080000 1.433333 1.300000 0.000000 0.000000
#[17] 0.000000 1.316667 2.383333 0.000000 1.383333 1.366667 2.433333 1.580000

预期输出有两个新列( time_expected time / time_expected

Expected output where two new columns (time_expected and time/time_expected)

  Field time   T Experiment  time_expected time/time_expected
1 Acute  0.0   0          A  6              0.0
2    An  9.0 120          A  6              1.5
3    En 15.6   2          A  6              2.6
4    Fo  9.2   2          A  5              1.84


推荐答案

我们可以使用 data.table 进行加入,并通过分配(:= )输出 time / time_expected )to'timeN'

We can do a join with data.table and create the new column by assigning (:=) the output of time/time_expected) to 'timeN'

setDT(DF)[DF2, time_expected := time_expected, on = .(Field)]
DF[, timeN := time/time_expected]
head(DF, 4)
#    Field time   T Experiment time_expected timeN
#1: Acute  0.0   0          A             6  0.00
#2:    An  9.0 120          A             6  1.50
#3:    En 15.6   2          A             6  2.60
#4:    Fo  9.2   2          A             5  1.84






或者可以在


Or it can be done within in the on

setDT(DF)[DF2, c('time_expected', 'timeN') := .(time_expected,time/time_expected), on = .(Field)]

DF
#   Field time   T Experiment time_expected    timeN
# 1: Acute  0.0   0          A             6 0.000000
# 2:    An  9.0 120          A             6 1.500000
# 3:    En 15.6   2          A             6 2.600000
# 4:    Fo  9.2   2          A             5 1.840000
# 5: Acute  8.3   1          B             6 1.383333
# 6:    An  7.7  26          B             6 1.283333
# 7:    En 12.9   1          B             6 2.150000
# 8:    Fo  0.0   0          B             5 0.000000
# 9: Acute  7.5   1          C             6 1.250000
#10:    An  7.9  43          C             6 1.316667
#11:    En  0.0   0          C             6 0.000000
#12:    Fo  5.4   1          C             5 1.080000
#13: Acute  8.6   2          D             6 1.433333
#14:    An  7.8  77          D             6 1.300000
#15:    En  0.0   0          D             6 0.000000
#16:    Fo  0.0   0          D             5 0.000000
#17: Acute  0.0   0          E             6 0.000000
#18:    An  7.9  60          E             6 1.316667
#19:    En 14.3   1          E             6 2.383333
#20:    Fo  0.0   0          E             5 0.000000
#21: Acute  8.3   4          F             6 1.383333
#22:    An  8.2 326          F             6 1.366667
#23:    En 14.6   4          F             6 2.433333
#24:    Fo  7.9   3          F             5 1.580000

其中'时'小于8

DF[time < 8]
#    Field time  T Experiment time_expected    timeN
# 1: Acute  0.0  0          A             6 0.000000
# 2:    An  7.7 26          B             6 1.283333
# 3:    Fo  0.0  0          B             5 0.000000
# 4: Acute  7.5  1          C             6 1.250000
# 5:    An  7.9 43          C             6 1.316667
# 6:    En  0.0  0          C             6 0.000000
# 7:    Fo  5.4  1          C             5 1.080000
# 8:    An  7.8 77          D             6 1.300000
# 9:    En  0.0  0          D             6 0.000000
#10:    Fo  0.0  0          D             5 0.000000
#11: Acute  0.0  0          E             6 0.000000
#12:    An  7.9 60          E             6 1.316667
#13:    Fo  0.0  0          E             5 0.000000
#14:    Fo  7.9  3          F             5 1.580000

这篇关于如何在这个R数据表上输出另一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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