在R中对数据的列进行排序。

4

我有一个包含141行和多个列的CSV文件。我希望将我的数据按照第一和第二列(即“label”和“index”)升序排序。以下是我的代码:

final_data <- read.csv("./features.csv",
                   header = FALSE,
                   col.names = c('label','index', 'nr_pix', 'rows_with_1', 'cols_with_1',
                                 'rows_with_3p', 'cols_with_3p', 'aspect_ratio',
                                 'neigh_1', 'no_neigh_above', 'no_neigh_below',
                                 'no_neigh_left', 'no_neigh_right', 'no_neigh_horiz',
                                 'no_neigh_vert', 'connected_areas', 'eyes', 'custom'))

sorted_data_by_label <- final_data[order(label),]
sorted_data_by_index <- sorted_data_by_label[order(index),]

write.table(sorted_data_by_index, file = "./features.csv",
            append = FALSE, sep = ',',
            row.names = FALSE)

我选择从CSV文件中读取,并使用write.table,因为这是我的代码要求,需要用列名覆盖CSV文件。

即使在order(label),order(index),后面添加了,,代码排序数据仍然应该能够读取其他行和列对吧?

运行这段代码后,我只得到了141行中的第一行。有没有办法解决这个问题呢?


你需要使用 final_data$label,并在第二个中使用 sorted_data_by_label$index,如果索引未在全局环境中创建为对象。 - akrun
1个回答

5
如 @akrun 简要提到的,您需要做的是更改
sorted_data_by_label <- final_data[order(label),]

sorted_data_by_label <- final_data[order(final_data$label),]
并更改
sorted_data_by_index <- sorted_data_by_label[order(index),]

sorted_data_by_index <- sorted_data_by_label[order(sorted_data_by_label$index),]
这是因为当您写入 label 时,R 将尝试在全局环境中查找 index 对象,而不在 final_data 数据框中。如果您打算使用 final_data 的列 index,则需要使用显式的 final_data$index。
其他选项:
您可以使用 with:
sorted_data_by_label <- with(final_data, final_data[order(label),])
sorted_data_by_index <- with(sorted_data_by_label, sorted_data_by_label[order(index),])
在 dplyr 中,您可以使用 arrange 函数:
sorted_data_by_label <- final_data %>% arrange(label)
sorted_data_by_index <- sorted_data_by_label %>% arrange(index)

1
明白了,它将索引作为全局环境。我现在已经修复了错误,谢谢。 - Dennis Main

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接