简体   繁体   English

如何在此R data.table输出上显示另一列?

[英]How to present another column on this R data.table output?

I am trying to present another columns DF2$time_expected and DF$time/DF2$time_expected in the output of DF[DF$Experiment=="A", ] . 我试图在DF[DF$Experiment=="A", ]的输出中显示另一列DF2$time_expectedDF$time/DF2$time_expected 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

Expected output where two new columns ( time_expected and time/time_expected ) 预期输出中有两个新列( time_expectedtime/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

It's usually not a good idea to load both data.table and dplyr together. data.tabledplyr在一起通常不是一个好主意。

With dplyr we can do: 使用dplyr我们可以:

library(dplyr) # No need to quote

final_df <- DF %>% 
  filter(time < 8) %>%
  full_join(DF2) %>% 
  mutate(`time/time_expected` = time / time_expected) %>% 
  select(Field, `time/time_expected`)

final_df
#> Joining, by = "Field"
#>    Field time  T Experiment time_expected time/time_expected
#> 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

We can do a join with data.table and create the new column by assigning ( := ) the output of time/time_expected ) to 'timeN' 我们可以使用data.table进行连接,并通过将( :=time/time_expected的输出分配给'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 或者,它可以在在完成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

subsetting the rows where 'time' is less than 8 对“时间”小于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对R data.table中的列列进行操作以输出另一个列表列? - How to do operations on list columns in an R data.table to output another list column? R的data.table中存在的列表列的排序元素 - Sorting elements of a list column present in R's data.table 我的另一列上的R data.table grepl列 - R data.table grepl column on another column in i 在R Data.Table中创建一个计数器列,并在另一列上创建条件 - Create a counter column in R Data.Table, with a condition on another column 根据另一列R data.table添加一列 - Adding a column based on another column R data.table 如何知道一个列是否是R data.table中另一个列的值的一部分的值 - How to know if the value if a column is part of another column's value in R data.table 如何将列添加到R中的data.table,该列基于另一列中的字符串? - How to add column to a data.table in R that is based on a string in another column? 如何将列添加到包含另一列组的所有可能组合的 R data.table - How to add a column to an R data.table that contains all the possible combinations by group of another column R data.table添加列作为另一个data.table的函数 - R data.table add column as function of another data.table R:尝试使用另一个data.table操作列时data.table中的奇怪行为 - R: strange behavior in data.table when attempting to operate on a column using another data.table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM