随机选择 R 数据表中的 50 列会导致只有 50 行的表格。如何修复这个问题?

huangapple go评论96阅读模式
英文:

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 &lt;- sample(colnames(mtcars)[-c(1, 2)], 3)

# Now combine the constant columns with the randomly select using `all_of()` function 
mtcars %&gt;%  
  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 &lt;- 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 &lt;- ogTable[, c(1:2, sample(3:ncol(ogTable), 50)), with = FALSE]

A reprex:

library(data.table)
set.seed(1)
test &lt;- data.table(iris)
randomTable &lt;- 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

huangapple
  • 本文由 发表于 2023年6月2日 05:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定