我有一个这样的数据框:
n = c(2, 2, 3, 3, 4, 4)
n <- as.factor(n)
s = c("a", "b", "c", "d", "e", "f")
df = data.frame(n, s)
df
n s
1 2 a
2 2 b
3 3 c
4 3 d
5 4 e
6 4 f
Run Code Online (Sandbox Code Playgroud)
我想访问我的因子的每个级别的第一个元素(在这个例子中有一个包含的向量a, c, e).
有可能达到一个级别的第一个元素
df$s[df$n == 2][1]
Run Code Online (Sandbox Code Playgroud)
但它并不适用于所有级别:
df$s[df$n == levels(n)]
[1] a f
Run Code Online (Sandbox Code Playgroud)
你会怎么做?
更进一步,我想修改我的数据框,看看哪个是每次出现的每个级别的第一个元素.在我的示例中,新列应该是:
n s rep firstelement
1 2 a a a
2 2 b c a
3 3 c …Run Code Online (Sandbox Code Playgroud) 通过提升,ES6模块在文档准备好之前加载.
例如,
// module.js
console.log('body', document.body);
export let a = 1;
// main.js
import {a} from 'module'
Run Code Online (Sandbox Code Playgroud)
登录body null控制台.
如何使用DOM操作并需要ES6模块document ready?
我试着用
$(document).ready(function() {
var a = 1;
});
export {a};
Run Code Online (Sandbox Code Playgroud)
在我的模块中但是babel给我一个Unexpected token错误.
我也试过了
$(document).ready(function() {
export let a = 1;
});
Run Code Online (Sandbox Code Playgroud)
我有一个'import' and 'export' may only appear at the top level错误.
更新:
我有同样的问题
document.addEventListener("DOMContentLoaded",function(){
var a = 1;
}
export {a};
Run Code Online (Sandbox Code Playgroud)
因为a没有定义.
也就是说要导出的变量不可用(请参阅我的更新).
更新:
这是我基于@MaciejSikora代码的尝试:
function Test() {
document.addEventListener("DOMContentLoaded",()=>{ …Run Code Online (Sandbox Code Playgroud) 我想使用tensorflow.js从 2D 张量获取数据。我尝试使用data()这样的方法:
const X = tf.tensor2d([[1, 2, 3, 4], [2, 2, 5, 3]]);\nX.data().then(X => console.log(X)};\nRun Code Online (Sandbox Code Playgroud)\n\n但结果是一个展平的一维数组:
\n\nFloat32Array(8)\xc2\xa0[1, 2, 3, 4, 2, 2, 5, 3]\nRun Code Online (Sandbox Code Playgroud)\n\n有没有办法保持数组的形状?
\n我使用PouchDB认证插件,我喜欢它.
但是,我对注册过程有点困惑,也许我没有以正确的方式使用它.我用它在我的CouchDB数据库中创建新用户但我不明白为什么它与特定数据库相关联?由于我在特定数据库之前创建了CouchDB用户,因此我必须使用虚拟数据库来创建新用户:
// Create or get a database on the CouchDB server to further create a new user
var remoteDB = new PouchDB('http://169.254.197.198:5984/dummy');
// Signup to this db in order to create new user in the CouchDB server
remoteDB.signUp(document.register.username.value,
document.register.password1.value).then(function (reponse){
}).then(function (reponse) {
// handle response
}).catch(function (error) {
// handle error
});
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法独立于数据库创建新用户?
我使用PouchDB离线存储数据,并在应用程序上线时与远程CouchDB数据库同步.如果应用程序在线启动,它可以正常工作:PouchDB pause在连接中断时触发事件,并在连接恢复时继续.这是一个示例代码:
localDB.replicate.to(remoteDB, {
live: true,
retry: true
}).on('paused', function (info) {
// replication was paused, usually because of a lost connection
}).on('change', function (change) {
// yo, something changed!
}).on('active', function (info) {
// replication was resumed
}).on('error', function (err) {
console.log(err);
// totally unhandled error (shouldn't happen)
})
Run Code Online (Sandbox Code Playgroud)
但是当脱机启动应用程序时,会遇到错误(Failed to load resource: net::ERR_ADDRESS_UNREACHABLE)
无法访问远程数据库,PouchDB触发error事件("Database encountered an unknown error"状态500).即使连接回来,复制也行不通.
问题:
即使应用程序是脱机启动的,也有办法使复制工作(例如,pause即使没有访问远程数据库,事件也会先发生)?
更新: 感谢nlawson的回答!我只需要在函数中添加远程数据库创建以使其工作:
function retryReplication() {
var remoteDB = new …Run Code Online (Sandbox Code Playgroud) 我需要将所有数据库中的所有文档的内容保存在一个文件中。我知道我可以使用curl从一个数据库获取所有文档:
curl -X GET http://localhost:5984/dbname/_all_docs?include_docs=true
Run Code Online (Sandbox Code Playgroud)
我对所有数据库尝试这个:
curl -X GET http://localhost:5984/_all_dbs/_all_docs?include_docs=true
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
couchdb ×3
javascript ×2
pouchdb ×2
curl ×1
database ×1
dom ×1
es6-modules ×1
get ×1
r ×1
r-factor ×1