亲爱的同事我有一个包含多个列的数据集,表明机器的数量和记录的变量数量,如下所示:
Machine Name1(A01) Name2(A02) Name3(A03) ... Name1(A01)
Signal Signal1 Signal 1 Signal 1 ... Signal124
Timestamp Unit Unit Unit Unit
2017-11-01 00:00 value1 value1
2017-11-01 00:30 value2 value2
2017-11-01 01:00 .
. .
. .
我想知道是否可以通过数字(Ayx)过滤signalX的值并将这些值存储在矩阵中。
基本上我必须存储以下值:
filter the values
Signal1[i,j]=
Signal2[i,j]=
我是机器的数量和时间戳
答案 0 :(得分:1)
如果我理解你的问题,那么其中一种方法可能是
library(tidyverse)
df1 <- t(df) %>%
data.frame(stringsAsFactors = F) %>%
rownames_to_column() %>%
`colnames<-`(.[1,]) %>%
filter(row_number()!=1) %>%
gather(Timestamp, signal_value, -Machine, -Signal, -Timestamp_unit)
将输出
> df1
Machine Signal Timestamp_unit Timestamp signal_value
1 A01 Signal1 Unit 2017-11-01 00:00 value1
2 A02 Signal1 Unit 2017-11-01 00:00 value1_1
3 A03 Signal2 Unit 2017-11-01 00:00 value1_2
4 A04 Signal124 Unit 2017-11-01 00:00 value1_3
5 A01 Signal1 Unit 2017-11-01 00:30 value2
6 A02 Signal1 Unit 2017-11-01 00:30 value2_1
7 A03 Signal2 Unit 2017-11-01 00:30 value2_2
8 A04 Signal124 Unit 2017-11-01 00:30 value2_3
9 A01 Signal1 Unit 2017-11-01 01:00 value3
10 A02 Signal1 Unit 2017-11-01 01:00 value3_1
11 A03 Signal2 Unit 2017-11-01 01:00 value3_2
12 A04 Signal124 Unit 2017-11-01 01:00 value3_3
现在,您可以轻松查询所需的值
df1[df1$Signal=='Signal1' & df1$Machine=='A02' & df1$Timestamp=='2017-11-01 00:00', "signal_value"]
#[1] "value1_1"
示例数据
df <- read.table(text="Machine A01 A02 A03 A04
Signal Signal1 Signal1 Signal2 Signal124
Timestamp_unit Unit Unit Unit Unit
'2017-11-01 00:00' value1 value1_1 value1_2 value1_3
'2017-11-01 00:30' value2 value2_1 value2_2 value2_3
'2017-11-01 01:00' value3 value3_1 value3_2 value3_3", header=T, stringsAsFactor=F)