英文:
Randomly selecting 50 columns in R data table results in table with only 50 rows. How can I fix this?
问题
我正在尝试在R中随机选择数据表中的50列(原始表格有110列和1000+行)。但是当我运行代码时,生成的表格只有50行(我不是试图筛选行,只是列)。
randomTable = sample_n(ogTable, 50, axis = 'columns')
我查找了这个问题,似乎这个函数如果超过了结果列的数量,就不会显示所有行,但我找不到解决方法。
英文:
I'm trying to randomly select 50 columns in a data table in R (the original table has 110 columns and 1000+ rows). But when I run the code, the resulting table only has 50 rows (I am not trying to filter out the rows, only the columns).
randomTable = sample_n(ogTable, 50, axis = 'columns')
I looked up this issue and it seems like this function doesn't display all rows if it exceeds the number of resulting columns, but I could not find a way to get around this.
答案1
得分: 0
更新后的解释:
library(dplyr)
set.seed(42)
# 从mtcars中随机选择3列,保持前两列不变
selected_columns <- sample(colnames(mtcars)[-c(1, 2)], 3)
# 使用`all_of()`函数将常数列与随机选择的列组合在一起
mtcars %>%
select(mpg, cyl, all_of(selected_columns))
第一个回答:
使用dplyr
中的sample_n
来随机抽取行(例如,观测值)。请参考?sample_n
:sample_n() 和 sample_frac() 已被slice_sample()
取代。
要随机选择50列,我们可以这样做:
randomTable <- ogTable[, sample(ncol(ogTable), 50)]
英文:
Update after clarification:
library(dplyr)
set.seed(42)
# Select 3 columns from mtcars randomly keeping the first two constant
selected_columns <- sample(colnames(mtcars)[-c(1, 2)], 3)
# Now combine the constant columns with the randomly select using `all_of()` function
mtcars %>%
select(mpg, cyl, all_of(selected_columns))
first answer:
sample_n
from dplyr
is used for sampling rows (e.g. observations). See ?sample_n
: sample_n() and sample_frac() have been superseded in favour of slice_sample()
.
We could do it this way to randomly select 50 columns:
randomTable <- ogTable[, sample(ncol(ogTable), 50)]
答案2
得分: 0
这个一行代码有效:
randomTable <- ogTable[, c(1:2, sample(3:ncol(ogTable), 50)), with = FALSE]
一个 reprex:
library(data.table)
set.seed(1)
test <- data.table(iris)
randomTable <- test[, c(1:2, sample(3:ncol(test), 2)), with = FALSE]
head(randomTable)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
英文:
This one-liner works:
randomTable <- ogTable[, c(1:2, sample(3:ncol(ogTable), 50)), with = FALSE]
A reprex:
library(data.table)
set.seed(1)
test <- data.table(iris)
randomTable <- test[, c(1:2, sample(3:ncol(test), 2)), with = FALSE]
head(randomTable)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论