将posixlt添加为数据帧的新列

时间:2017-05-05 08:15:21

标签: r

我正在创建一些随机数字:

My code is:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG="MyMessageservice";
    RemoteMessage remoteMessage;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage){
        String title=remoteMessage.getNotification().getTitle();
        String message=remoteMessage.getNotification().getBody();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       // String click_action=remoteMessage.getNotification().getClickAction();
        Intent intent=new Intent(this,VisitorList.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri notificattionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this);
        notificationbuilder.setContentTitle(title);
        notificationbuilder.setContentText(message);
        notificationbuilder.setAutoCancel(true);
        notificationbuilder.setSound(notificattionSound);
        notificationbuilder.setSmallIcon(R.drawable.ic_launcher);
        notificationbuilder.setContentIntent(pendingIntent);
        notificationbuilder.setAutoCancel(true);
        notificationManager.notify(0, notificationbuilder.build());
    }
}

我想创建一个相应的时间戳列并将其绑定到上面的数据框。

data <- matrix(runif(10, 0, 1), ncol = 2)
dataframe <- data.frame(data)

> dataframe
         X1         X2
1 0.7981783 0.13233858
2 0.9592338 0.05512942
3 0.1812384 0.74571334
4 0.1447498 0.96656930
5 0.1735390 0.37345575

这会创建10个值。

time <- as.POSIXlt(runif(10, 0, 60), origin = "2017-05-05 10:00:00")

现在,我想将它绑定到数据帧,所以我首先想到它是一个矩阵:

> time
 [1] "2017-05-05 13:00:27 EEST" "2017-05-05 13:00:02 EEST" "2017-05-05 13:00:26 EEST" "2017-05-05 13:00:25 EEST" "2017-05-05 13:00:28 EEST"
 [6] "2017-05-05 13:00:17 EEST" "2017-05-05 13:00:35 EEST" "2017-05-05 13:00:08 EEST" "2017-05-05 13:00:29 EEST" "2017-05-05 13:00:32 EEST"

但是这给了我:

time <- matrix(time, nrow = 5, ncol = 2)

1 个答案:

答案 0 :(得分:2)

原因是POSIXlt将日期时间存储为list个属性,而POSIXct则不存在。因此,最好使用as.POSIXct

time <- as.POSIXct(runif(10, 0, 60), origin = "2017-05-05 10:00:00")

如果我们需要存储,可以list data.frame

完成
data.frame(date1= time[1:5], date2 = time[6:10])

未转换为matrix作为&#39;日期时间&#39;转换为integer时,会被强制转换为matrix存储模式。

假设我们继续POSIXlt,然后我们找到list属性

time1 <- as.POSIXlt(runif(10, 0, 60), origin = "2017-05-05 10:00:00")
unclass(time1)
#$sec
# [1] 13.424695 40.860449 57.756890 59.072140 24.425521 39.429729 58.309546
# [8]  6.294982 46.613436 25.444415

#$min
# [1] 30 30 30 30 30 30 30 30 30 30

#$hour
# [1] 15 15 15 15 15 15 15 15 15 15

#$mday
# [1] 5 5 5 5 5 5 5 5 5 5

#$mon
# [1] 4 4 4 4 4 4 4 4 4 4

#$year
# [1] 117 117 117 117 117 117 117 117 117 117

#$wday
# [1] 5 5 5 5 5 5 5 5 5 5

#$yday
# [1] 124 124 124 124 124 124 124 124 124 124

#$isdst
# [1] 0 0 0 0 0 0 0 0 0 0

#$zone
# [1] "IST" "IST" "IST" "IST" "IST" "IST" "IST" "IST" "IST" "IST"

#$gmtoff
# [1] 19800 19800 19800 19800 19800 19800 19800 19800 19800 19800

#attr(,"tzone")
#[1] ""    "IST" "IST"

使用POSIXctunclass

可以找到整数存储值
 unclass(time)
 #[1] 1493978445 1493978451 1493978432 1493978402 1493978447 1493978441
 #[7] 1493978445 1493978450 1493978419 1493978425
 #attr(,"tzone")
 #[1] ""