我有一个像这样的数据集:
record <- c("2000-01-22","2001-01-22","2002-01-22")
brand <- c("blue","red","yellow")
sale <- c("5","42","54")
mydf <- data.frame(record,brand,sale)
mydf
record brand sale
1 2000-01-22 blue 5
2 2001-01-22 red 42
3 2002-01-22 yellow 54
我想创建一个名为&#34; year&#34;的新列。仅包括不同的年份,例如:
record brand sale year
1 2000-01-22 blue 5 2000
2 2001-01-22 red 42 2001
3 2002-01-22 yellow 54 2002
非常感谢您的帮助。
巴兹
答案 0 :(得分:1)
您可以使用lubridate::year
:
library(lubridate)
mydf$year <- year(as.Date(mydf$record))
> mydf
record brand sale year
1 2000-01-22 blue 5 2000
2 2001-01-22 red 42 2001
3 2002-01-22 yellow 54 2002
答案 1 :(得分:1)
不使用其他包。 <怎么样
public class Point3D {
public int x, y, z;
public static List<Point3D> allWithinRange(List<Point3D> possiblePoints, int x, int y, int z, int inter) {
List<Point3D> list = new ArrayList<Point3D>(possiblePoints.size());
possiblePoints.stream()
.filter(it -> it.x <= x + inter && it.x >= x - inter)
.filter(it -> it.y <= y + inter && it.y >= y - inter)
.filter(it -> it.z <= z + inter && it.z >= z - inter)
.forEach(list::add);
return list;
}
}