我是R的新手,在下面需要帮助。 我有此数据:
$soJSON = "<div id='demo'></div>";
$soArray = json_decode($soJSON, true);
echo $soArray["contacts"][0]["givenName"];
我需要创建一个数据框“ minute_data”。每行对应一分钟的观测值,其中包含该分钟内的事件计数和温度。
某些分钟可能不包含任何事件。与此类分钟对应的行应从数据框中排除。
数据框应该看起来像(前2行):
Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218
答案 0 :(得分:1)
您可以使用dplyr
:
library(dplyr)
df %>%
mutate(minute = Time %/% 60 + 1) %>%
group_by(minute) %>%
summarise(count = n(),
temperature = first(temperature)) %>%
select(-Time)
注意:如果您想要卑鄙的话,请将first
更改为mean
答案 1 :(得分:1)
(当然)data.table
也可以执行此操作
样本数据
library( data.table )
DT <- fread("Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218")
代码
这实际上是单行代码,但是出于可读性考虑,我添加了一些换行符。
DT[, list( count = .N,
temperature = mean( Temperature ) ),
by = .( minute = floor( Time / 60 ) + 1 )]
输出
# minute count temperature
# 1: 1 7 96.32332
# 2: 2 15 100.47791
# 3: 3 3 92.70218