Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何向数据帧动态添加数据?_R_Sqlite_Dataframe_Rsqlite - Fatal编程技术网

R 如何向数据帧动态添加数据?

R 如何向数据帧动态添加数据?,r,sqlite,dataframe,rsqlite,R,Sqlite,Dataframe,Rsqlite,我有文件中每一行需要清理的数据,我想将清理后的数据插入SQLite3数据库。我正在使用图书馆,它需要一个。以下是我尝试使用的代码: # Select feature names for use as column names in X train/test loading feature_names <- unlist(dbGetQuery(con, "select feature_name from features order by feature_id"), use.names =

我有文件中每一行需要清理的数据,我想将清理后的数据插入SQLite3数据库。我正在使用图书馆,它需要一个。以下是我尝试使用的代码:

# Select feature names for use as column names in X train/test loading
feature_names <- unlist(dbGetQuery(con, "select feature_name from features order by feature_id"), use.names = FALSE);

# Load X training data
X_train_lines <- readLines("data/train/X_train.txt"); # Space delimited with leading and trailing spaces
X_train_values <- vector("list", length(X_train_lines));
names(X_train_values) <- feature_names; # colnames or names?
for (index in 1:length(X_train_lines)) {
  cleaned_line <- gsub("^ *|(?<= ) | *$", "", X_train_lines[index], perl=TRUE); # remove extraneous whitespaces
  X_train_values[index] <- strsplit(cleaned_line, " "); # Wondering if X_train_values[index] is correct? 
}
# Write features data to features table
dbWriteTable(con, "X_train", as.data.frame(X_train_values), row.names = FALSE);
再一次,这让我相信数据是完全混淆的。它应该有561列,其中一些列在上面表示为tBodyAcc-mean()-X等。列值应该是上面没有看到的浮点数

table命令不起作用:

table(X_train_values)
Error in table(X_train_values) : 
  attempt to make a table with >= 2^31 elements
我应该有7352行561列

更新2

我认为我的问题是我试图使用一个或多个数组之类的列表。例如,在Ruby中,我可以这样做:

x_train_values = []
x_train_lines.each { |line| x_train_values << line.split(' ') }
x\u列值=[]
x|u列。以下各列中的每个{列| x|u列|值

for (index in 1:length(X_train_lines)) {
    cleaned_line <- gsub("^ *|(?<= ) | *$", "", X_train_lines[index], perl=TRUE);
    X_train_values[index] <- strsplit(cleaned_line, " ");
}

即使是部分的
dput(X\u列值)
也很容易处理。你不需要for循环在向量上执行gsub,只需
gsub(“旧”、“新”,向量)
将起作用如果您创建一个列表,然后调用一个强制到数据帧的函数,那么每个列表元素都将成为一列,因此它就像
apply(dat,1,…)发生的“换位”一样
。如果您使用正在使用的软件包中的一个内置数据库构建了一个示例,那么人们可能会提供更多帮助。感谢您的澄清/提示!我之所以使用循环,是因为我的X\U train数据文件是以空格分隔的,但也可能为每列提供额外的前导空格以容纳减号。Onc我已经去掉了多余的空格,我将行按空格分割,以获得正确的列值。然后我尝试将其强制为data.frame,以便将其保存到数据库中。这并不理想,但我想不出其他方法,因为我是R新手。好的,我明白了……我认为您的
for
循环无论如何都应该可以工作,只要将
[
替换为
[[
X\u列车线[index]
中。我只是建议
应用
作为一种更“友好”的方法。但是如果它对您有效,您可以保留
for
循环。
x_train_values = []
x_train_lines.each { |line| x_train_values << line.split(' ') }
for (index in 1:length(X_train_lines)) {
    cleaned_line <- gsub("^ *|(?<= ) | *$", "", X_train_lines[index], perl=TRUE);
    X_train_values[index] <- strsplit(cleaned_line, " ");
}
apply(X_train_values, 2, gsub, pattern = "^ *|(?<= ) | *$",
    replacement = "", perl = T)