更新了数据集:
DateTime Object.Name Object.Value
6/22/2017 21:11 DaHum Normal
6/22/2017 12:59 DaHum Alarm
6/16/2017 18:48 DaHum Normal
6/16/2017 14:33 DaHum Alarm
6/15/2017 18:46 DaHum Normal
7/28/2017 8:00 ZON-1 58.56
7/28/2017 8:00 MA-H 51.66
7/28/2017 8:00 ZON-2 72.00
7/28/2017 8:00 ZON-4 70.00
7/28/2017 8:00 ZON-3 72.00
7/28/2017 7:45 PH 0.00
7/28/2017 7:45 OA 79.50
7/28/2017 7:45 SP 50.00
7/28/2017 7:45 ZON-1 32.47
7/28/2017 7:45 ZON-3 70.00
7/28/2017 7:45 CC 55.81
您好我有以下格式的数据框:
我需要将Object_Name
下的所有值转换为列名。 Object_Names具有重复值,即the same Name is repeated with different timestamp.
Object_Value
的数据类型是字母数字,因此在R中传递时,它可以作为Factor or a Character
因此,基于时间戳,我需要转换所有Object_Name row values to column name
Date Time Object_Name Object_Value
7/28/2017 08:00 A1 58.56
7/28/2017 08:00 A2 51.66
.
.
.
7/28/2017 08:30 A1 60.2
7/28/2017 08:30 A2 65.2
.
.
7/30/2017 08:30 B1 On
7/30/2017 09:30 B1 Off
我需要output
,如下所示:
Date Time A1 A2 B1
7/28/2017 08:00 58.5 51.6 -
7/28/2017 08:30 60.2 65.2 -
7/30/2017 08:30 - - On
7/30/2017 09:30 - - Off
到目前为止代码:
JCI <- read.csv("JCIS2.csv",header = T, stringsAsFactors=FALSE)
JCI$Object.Value <- as.numeric(JCI$Object.Value)
library(reshape2)
JCI_Reshape <- dcast(JCI_Unique, Date...Time ~ Object.Name, value.var = "Object.Value", fun.aggregate = mean)
答案 0 :(得分:0)
library(tidyr)
spread(JCI, Object_Name,Object_Value)
Date Time A1 A2 B1
1: 7/28/2017 08:00 58.56 51.66 NA
2: 7/28/2017 08:30 60.2 65.2 NA
3: 7/30/2017 08:30 NA NA On
4: 7/30/2017 09:30 NA NA Off
数据:
dput(JCI)
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("7/28/2017",
"7/30/2017"), class = "factor"), Time = structure(c(1L, 1L, 2L,
2L, 2L, 3L), .Label = c("08:00", "08:30", "09:30"), class = "factor"),
Object_Name = structure(c(1L, 2L, 1L, 2L, 3L, 3L), .Label = c("A1",
"A2", "B1"), class = "factor"), Object_Value = structure(c(2L,
1L, 3L, 4L, 6L, 5L), .Label = c("51.66", "58.56", "60.2",
"65.2", "Off", "On"), class = "factor")), .Names = c("Date",
"Time", "Object_Name", "Object_Value"), class = c("data.table",
"data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x0000000007b70788>)