在Tibble中从第1行中减去第2行

时间:2017-09-20 04:25:30

标签: r dataframe tibble

我在R中有一个tibble对象,如下所示: enter image description here

对于每一列,我想从row2中减去row1。我目前的做法如下:

09-20 08:39:39.891 24773-24773/com.example.khan_pti.mfetchdata I/art: Not late-enabling -Xcheck:jni (already on)
09-20 08:39:40.073 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.307 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.346 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.391 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.443 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.511 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.560 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.603 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.635 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.677 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.713 24773-24773/com.example.khan_pti.mfetchdata W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.khan_pti.mfetchdata-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.khan_pti.mfetchdata-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
09-20 08:39:40.714 24773-24773/com.example.khan_pti.mfetchdata W/System: ClassLoader referenced unknown path: /data/app/com.example.khan_pti.mfetchdata-2/lib/x86
09-20 08:39:40.721 24773-24773/com.example.khan_pti.mfetchdata I/InstantRun: starting instant run server: is main process
09-20 08:39:41.005 24773-24831/com.example.khan_pti.mfetchdata D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                 [ 09-20 08:39:41.010 24773:24773 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xab5c20c0, tid 24773


                                                                                 [ 09-20 08:39:41.069 24773:24831 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xab5c2880, tid 24831
09-20 08:39:41.076 24773-24831/com.example.khan_pti.mfetchdata I/OpenGLRenderer: Initialized EGL, version 1.4
09-20 08:39:41.166 24773-24773/com.example.khan_pti.mfetchdata W/art: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
09-20 08:39:44.907 24773-24888/com.example.khan_pti.mfetchdata D/testhtt2: test
09-20 08:39:44.919 24773-24888/com.example.khan_pti.mfetchdata D/test44: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("457d15ccad5389221d0935d83ac973ce");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://yakhforosh.skyf.ir/customers_mysql.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
09-20 08:39:44.935 24773-24831/com.example.khan_pti.mfetchdata E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaeff1000
09-20 08:39:44.946 24773-24831/com.example.khan_pti.mfetchdata D/OpenGLRenderer: endAllStagingAnimators on 0xb30a1b80 (RippleDrawable) with handle 0xaefd2d20
09-20 08:39:44.974 24773-24773/com.example.khan_pti.mfetchdata W/System.err: org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONArray
09-20 08:39:44.974 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
09-20 08:39:44.974 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
09-20 08:39:44.974 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
09-20 08:39:44.974 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at com.example.khan_pti.mfetchdata.Fetch$BackTask.onPostExecute(Fetch.java:52)
09-20 08:39:44.974 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at com.example.khan_pti.mfetchdata.Fetch$BackTask.onPostExecute(Fetch.java:34)
09-20 08:39:44.975 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:651)
09-20 08:39:44.975 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at android.os.AsyncTask.-wrap1(AsyncTask.java)
09-20 08:39:44.975 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
09-20 08:39:44.975 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
09-20 08:39:44.975 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-20 08:39:44.976 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
09-20 08:39:44.976 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-20 08:39:44.976 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-20 08:39:44.976 24773-24773/com.example.khan_pti.mfetchdata W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

令人困惑的是,要在2x9660 tibble上执行此语句,代码需要8-15秒,由Sys.time()计算确定。感谢任何帮助,使这更快,并理解为什么这个命令需要这么长时间。

1 个答案:

答案 0 :(得分:2)

通过unlist,我们可以将其转换为vector并提高效率

unlist(each.group.mean[1,2:num.groups], use.names = FALSE)-
    unlist(each.group.mean[2,2:num.centroids], use.names = FALSE)

原因是data.frametibble等具有许多类属性,这使得执行子集然后计算变得更慢。通过转换为vector,所有这些类都被剥离了。使用可重现的例子

数据

set.seed(24)
df1 <- as.tibble(as.data.frame(matrix(sample(1:10, 9660*10, 
     replace = TRUE), ncol = 9660)))

基准

system.time(df1[1,] - df1[2,])
#   user  system elapsed 
#   0.78    0.00    0.78 

system.time(unlist(df1[1,], use.names = FALSE) - unlist(df1[2,], use.names = FALSE))
#   user  system elapsed 
#   0.03    0.00    0.03