public class P
{
public string Name {get; set;}
public string Value {get; set;}
public Student Student {get; set;}
}
List<P> p = new List<P>();
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "2", Value = null, Student = new Student{Name = "2"}})
p.Add(new P{Name = "1", Value = "xxx", Student = new Student{Name = "2"}})
我想按 P.Name 进行分组,只获取具有P.Value所有缺失值的元素。
在这个例子中我想得到“1”
如果我需要分组 P.Student.Name ?
答案 0 :(得分:5)
您可以将require(dplyr)
# manipulate the data
pdf <- df %>%
mutate(id = row_number()) %>%
gather(key, value, -id) %>%
group_by(key) %>%
mutate(min = min(value),
max = max(value),
mean = if_else(key != 'x1', mean(value), value),
norm = (mean-min)/(max-min))
# Draw plot
ggplot(pdf, aes(x = key, y = norm, group = id)) +
geom_path(aes(color = id), alpha = 0.5, lineend = 'round', linejoin = 'round')
与GroupBy
一起用于每个群组:
All
修改强>
如果您需要按var r = p.GroupBy(x => x.Name)
.Where(g => g.All(x => x.Value == null))
.Select(g=> g.Key);
进行分组,则只需在Student.Name
中指定该属性即可。您可以根据要用于分组的项目指定任何表达式。
GroupBy