我正在学习R中的dplyr包,我真的很喜欢它.但现在我在处理数据中的NA值.
我想用相应小时的平均值替换任何NA,例如使用这个非常简单的示例:
#create an example
day = c(1, 1, 2, 2, 3, 3)
hour = c(8, 16, 8, 16, 8, 16)
profit = c(100, 200, 50, 60, NA, NA)
shop.data = data.frame(day, hour, profit)
#calculate the average for each hour
library(dplyr)
mean.profit <- shop.data %>%
group_by(hour) %>%
summarize(mean=mean(profit, na.rm=TRUE))
> mean.profit
Source: local data frame [2 x 2]
hour mean
1 8 75
2 16 130
Run Code Online (Sandbox Code Playgroud)
我是否可以使用dplyr transform命令将利润中的第3天的NA替换为75(8:00)和130(16:00)?
有没有办法获得所有唯一的键名称,而无需在jq外调用唯一的排序?
示例文件:
{"a": 1, "b": 2, "c": 3}
{"a": 4, "b": 5, "d": 6}
Run Code Online (Sandbox Code Playgroud)
和jq和sort命令一样,我现在使用它,但我认为它不是那么有效:
jq -r keys[] example | sort -u
a
b
c
d
Run Code Online (Sandbox Code Playgroud) 我是python的新手,有人知道什么是一个好方法?我可以编写脚本,但使用包可能会更快.
我有这个.csv文件(gigabytes large):
name, value, time
A, 1, 10
B, 2, 10
C, 3, 10
C, 3, 10 (should ignore duplicates, or non complete (A,B,C) entries
A, 4, 12 (should be sorted by time, this entry should be at the end, after time==11)
B, 5, 12
C, 6, 12
B, 7, 11 (order of A,B,C might be different)
C, 8, 11
A, 9, 11
Run Code Online (Sandbox Code Playgroud)
将其转换为新的.csv文件,其中包含:
time, A, B, C
10, 1, 2, 3
11, 9, 7, 8
12, 4, 5, …Run Code Online (Sandbox Code Playgroud)