给定一个字符向量,我想循环一个带有名称赋值的函数。
uprop
是一个“data.frame”(1000个观察值和20个列),如下面的输出所示:
> class(uprop)
[1] "data.frame"
部门,来源,目标和 WeightCount 都是uprop
中的所有列名称
让我们说我们需要简化这个重复的任务:
CAST_uprop_data <- subset(uprop, Department == "CAST", select = c(Source, Target, WeightCount))
CHEG_uprop_data <- subset(uprop, Department == "CHEG", select = c(Source, Target, WeightCount))
PHYS_uprop_data <- subset(uprop, Department == "PHYS", select = c(Source, Target, WeightCount))
此处CAST_uprop_data
也是data.frame。 (100个观察和3列)
我可以创建一个带有字符名称的矢量变量cust_dept_list
:
cust_dept_list <- c('CAST', 'CHEG', 'PHYS')
但是,我无法弄清楚如何遍历名称并让它运行并分配每个名称?
这是我的尝试:
for (i in c(cust_dept_list)){
print(paste0(i,"_uprop_data")) <- subset(uprop, Department == i, select = c(Source, Target, WeightCount)), i
}
提前感谢您帮助新手。
答案 0 :(得分:3)
不要创造一堆不同的变量;使用
创建值列表cust_dept_list <- c('CAST', 'CHEG', 'PHYS')
uprop_data <- lapply(cust_dept_list, function(x)
subset(uprop, Department == x, select = c(Source, Target, WeightCount))
)
然后您可以使用
访问data.framesuprop_data[["CAST"]]
uprop_data[["CHEG"]]
...
并且可以更容易地将函数循环到列表中的这些数据集以供将来分析。请参阅how do I make a list of data.frames
上的相关回复答案 1 :(得分:1)
在极少数情况下,您应该通过循环子集来分配全局变量。我建议学习tidyverse。
如果您对下面的内容一无所知,请查阅,因为%&gt;%运算符可以为您节省大量时间和精力(同时为其他人提供可读代码)。
你将使用&#34; tibble&#34;这与数据帧非常相似。在此范围内,您只需按部门分组,并创建一个包含其中所有数据的单独行!
> grouped_data
# A tibble: 3 x 2
Department data
<fctr> <list>
1 CAST <tibble [1,000 x 3]>
2 CHEG <tibble [1,000 x 3]>
3 PHYS <tibble [1,000 x 3]>
结果如下:
for(dept in unique(grouped_data$Department)){
print(dept)
print("###########################")
print(
grouped_data %>%
filter(Department == dept) %>%
unnest()
)
}
如果你需要在for循环中出于某种原因打印所有这些(对于每个部门来说看起来很粗糙1000行),它将如下所示:
[1] "CAST"
[1] "###########################"
# A tibble: 1,000 x 4
Department Source Target WeightCount
<fctr> <dbl> <dbl> <dbl>
1 CAST -0.3781853 -0.59457662 0.2796963
2 CAST 0.7261541 -1.06344758 1.1874874
3 CAST -0.1207312 0.56961950 0.2082236
4 CAST -1.5467661 1.23693964 -0.9732976
5 CAST -1.6626831 0.09252543 -0.3003913
6 CAST -0.2783635 -0.84363946 2.0588511
7 CAST 1.6981061 0.13755764 -0.3935691
8 CAST 0.4900337 -0.73662209 0.8861508
9 CAST 0.3971949 -0.23047428 1.6226582
10 CAST 0.7721574 -0.69117961 -0.4547899
# ... with 990 more rows
[1] "CHEG"
[1] "###########################"
# A tibble: 1,000 x 4
Department Source Target WeightCount
<fctr> <dbl> <dbl> <dbl>
1 CHEG -0.7843984 -0.8788216 0.60030359
2 CHEG -0.5636669 -2.2283878 -0.16178492
3 CHEG 0.9024084 -1.5052453 -1.58803972
4 CHEG 1.7662237 1.2125255 -0.91229428
5 CHEG 0.3950654 -0.8283651 0.07402481
6 CHEG 0.3928973 -1.3650744 -0.75262682
7 CHEG 1.1298127 1.4765888 -0.76059162
8 CHEG 0.4787867 0.6041770 -1.23313321
9 CHEG -1.4474401 -0.6747809 0.78431441
10 CHEG 0.6463868 0.2558378 -1.34131546
# ... with 990 more rows
[1] "PHYS"
[1] "###########################"
# A tibble: 1,000 x 4
Department Source Target WeightCount
<fctr> <dbl> <dbl> <dbl>
1 PHYS 0.1425978 -1.01397581 -0.16573546
2 PHYS -1.2572684 -1.13069956 -0.61870063
3 PHYS 1.2089882 1.51020970 -1.43474343
4 PHYS -0.6357010 -0.07362852 0.06683348
5 PHYS -1.6402587 -1.35273300 0.14436313
6 PHYS -0.9408105 -1.52515527 -0.06860152
7 PHYS 0.3143868 0.11814597 -0.37823801
8 PHYS -0.3232879 0.15408677 -0.62820531
9 PHYS 0.3152122 -0.72634466 -1.71955337
10 PHYS 0.7268282 -0.20872075 0.30780981
# ... with 990 more rows
返回:
<button style="align:right;font-size:13px" class="btn btn-info" id="show_hide_bt" style="background-color:#00b0ff;"><i
class="fa fa-eye"></i> Show</button>