R:如何同时按条件和随机对数据帧进行子集化?

邦巴代罗

我正在为一个子集功能而苦苦挣扎,希望我能从云端得到一些帮助。

在我的数据集中,surveydata可以找到列landscape我需要选择两种类型的所有景观7,并5与随机选择50从每个景观类型的对象36然后我想创建一个subsurveydata数据框中调用的新变量它应该包含一个数字,例如,1如果在上一个选择中选择了对象,0(或NA)如果不是。

最好我搜索一个基本的 R 解决方案,但我不坚持。

我提供了一个随机数据集以便更好地理解。

#create data
surveydata <- as.data.frame(replicate(6,sample(0:1,1000,rep=TRUE)))
# change values of columns
surveydata$V3 <- sample(7, size = nrow(surveydata), replace = TRUE)
surveydata$V4 <- sample(5, size = nrow(surveydata), replace = TRUE)
surveydata$V5 <- sample(5, size = nrow(surveydata), replace = TRUE)
surveydata$V6 <- sample(5, size = nrow(surveydata), replace = TRUE)
#create column with same distribution of values
surveydata$group <- c(1,2)
# rename columns
colnames(surveydata)[1] <- "gender"
colnames(surveydata)[2] <- "expert"
colnames(surveydata)[3] <- "landscape"
colnames(surveydata)[4] <- "q1"
colnames(surveydata)[5] <- "q2"
colnames(surveydata)[6] <- "q3"
艾伦卡梅伦

这是一个 R 方法,它使用采样和索引来实现结果:

# Sample index of rows where landscape is 3 or 6
ss <- sample(with(surveydata, which(landscape == 6 | landscape == 3)), 50, FALSE)

# Append index of all rows where landscape is 5 or 7
ss <- c(ss, with(surveydata, which(landscape == 5 | landscape == 7)))

# Create subset data frame
subset <- surveydata[ss, ]

# Create sub column to show which rows have been sampled
surveydata$sub <- numeric(nrow(surveydata))
surveydata$sub[ss] <- 1

# test result of creating sub column
head(surveydata)
#>   gender expert landscape q1 q2 q3 group sub
#> 1      0      1         7  1  5  3     1   1
#> 2      1      1         5  2  2  3     2   1
#> 3      0      0         4  5  5  2     1   0
#> 4      0      0         3  5  5  4     2   0
#> 5      0      1         7  1  5  1     1   1
#> 6      1      0         7  5  1  1     2   1

# ensure subsetted data frame is as expected
head(subset)
#>     gender expert landscape q1 q2 q3 group
#> 348      0      0         6  5  4  2     2
#> 333      1      1         6  4  2  4     1
#> 521      1      0         6  1  5  5     1
#> 522      1      0         6  4  5  2     2
#> 563      0      1         6  2  4  2     1
#> 13       0      0         6  5  2  4     1

reprex 包(v0.3.0)于 2020 年 7 月 8 日创建

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何按不符合条件的行名对数据帧进行子集化?

如何基于R中的条件对数据帧进行子集

如何像R中的SAS那样按列名对数据帧进行子集化?

根据R中的某些条件对数据帧进行子集

如何根据用户指定的条件对数据帧进行子集

如何使用占位符在 r 中对数据帧进行子集化

如何在R中使用grep选择精确匹配来对数据帧进行子集化

如何使用选择输入在 r Shiny 中对数据帧进行子集化?

什么策略建议按月和年对数据帧进行子集化以获得R中的单个数据帧列表

如何按日期对数据进行子集化并在R中执行多项操作?

如何根据R中列名中的数字条件对数据进行子集化?

如何根据多个联合条件在 R 中对数据框进行子集化

如何基于适用于大量列的“不等于”条件对数据帧进行子集化?

如何基于R中的条件组合对数据进行子集

如何对数据集进行子集化和应用

如何在R Shiny中对数据帧进行条件格式化?

如何根据下一次出现在R中对数据帧进行子集化?

如何基于向量对数据帧进行子集化,然后在dplyr中进行分组

通过数据的间隔,用 R 对数据帧进行子集化

使用lapply并通过特征和功能对数据帧进行子集化

根据向量列表对数据帧进行子集化

使用向量作为参数对数据帧进行子集化

使用 filter() 对数据帧进行子集化

在函数内对数据帧进行子集化

为什么我可以使用3维在R中对数据帧进行子集化?

如何使用“starts_with”函数对数据帧进行子集化?

如何基于有限列的选定变量对数据帧进行子集化?

存在NA时如何在因子水平上对数据帧进行子集化

在R中的ggplot中使用相同的列名称对数据帧进行子集和绘图