【发布时间】:2018-01-27 15:24:41
【问题描述】:
我对 R 中的循环很陌生,所以如果在其他地方问过这个问题,我深表歉意。
读入所有 30 个 CSV 文件 -> 将文件 A 物种与其他 30 个 CSV 文件按物种进行比较 -> 为 30 个文件中的每一个仅使用匹配物种编写一个新的 CSV 文件
文件 A 有一列包含 190 个物种的名称 ($name)。其他 30 个 csv 文件每个都有一列,其中包含物种 ($SBSname),在列 $SBSname 中的物种数量不同,其范围可以从 100-500 到重复(因此文件 CSV 文件可以大于 190 行) .但是我不知道如何编写...
这就是我目前所拥有的一切......
我已经循环了所有的 CSV 文件:
30files = list.files(pattern="*.csv")
for (i in 1:length(30files)) assign(30files[i], read.csv(30files[i]))
我的代码仅用于将一个 CSV 文件 (branching.csv) 与文件 A 进行比较:
> str(FileA)
'data.frame': **190 obs. of 1 variable**:
$ name: Factor w/ 190 levels "Acaena novae zelandiae",..: 1 2 3 4 5 6 7 8 9 10 ...
> str(branching.csv)
'data.frame': **4055 obs. of 7 variables:**
$ SBSname : Factor w/ 2877 levels "Abies alba","Abies nordmanniana",..: 794 2075 1049 162 132 333 541 1840 272 1553 ...
$ SBS.number : int 16443 26711 40171 40398 40867 41151 37871 42412 35847 36245 ...
$ general.method : Factor w/ 5 levels "derivation from morphologies or other plant traits",..: 3 1 2 2 2 2 2 2 2 2 ...
$ branching : Factor w/ 2 levels "no","yes": 2 2 1 1 1 1 1 1 1 1 ...
$ valid : int 1 1 1 1 1 1 1 1 1 1 ...
$ reference : Factor w/ 6 levels "Barkman, J.J.(1988): New systems of plant growth forms and phenological plant types",..: 1 1 3 3 3 3 3 3 3 3 ...
$ original.reference: Factor w/ 97 levels "Aarssen, L.W. (1981): The biology of Canadian weeds. 50. Hypochoeris radicata L.",..: 9 9 20 3 3 3 3 3 33 33 ...
Species<-branching.csv[(branching.csv$SBSname %in% FileA$name),]
write.csv(Species, file = "Branching.csv")
> str(Species)
'data.frame': **298 obs. of 7 variables:**
$ name : Factor w/ 2877 levels "Abies alba","Abies nordmanniana",..: 1049 162 1548 47 57 1647 1060 2788 2094 1976 ...
$ SBS.number : int 40171 40398 36280 40532 41629 42495 40103 32792 32892 30583 ...
$ general.method : Factor w/ 5 levels "derivation from morphologies or other plant traits",..: 2 2 2 2 2 2 2 2 2 2 ...
$ branching : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 2 1 2 ...
$ valid : int 1 1 1 1 1 1 1 1 1 1 ...
$ reference : Factor w/ 6 levels "Barkman, J.J.(1988): New systems of plant growth forms and phenological plant types",..: 3 3 3 3 3 3 3 3 3 3 ...
$ original.reference: Factor w/ 97 levels "Aarssen, L.W. (1981): The biology of Canadian weeds. 50. Hypochoeris radicata L.",..: 20 3 33 33 33 33 33 44 44 44 ...
任何帮助或建议都会很棒。不一定是循环!
【问题讨论】:
-
“$SBSname 列中的物种数量不同”是什么意思?在不同的行中可以有相同物种的重复,或者在 $SBSname 的条目上可以有多个(比如“|”分隔的条目)?
-
所以 $SBSname 可以有重复的物种 - 所以在不同行中相同物种的重复 示例:SBS 名称 Abutilon theophrasti Acer campestre Acer campestre Acer campestre Acer campestre Acer campestre Acer campestre Acer negundo Acer pseudoplatanus Acer pseudoplatanus Acer pseudoplatanus Acer pseudoplatanus Aceras anthropophorum Aceras anthropophorum Achillea millefolium
-
当你说“只用匹配的物种写 30 个文件”时,这是否意味着我应该只写一列,每行出现在 FileA 中的所有(唯一)物种名称?
-
FileA 只有一列的 $names,包含 190 个物种。 File1 有 6 列,其中一列是 $names,其中包含 300 个物种。如果物种 X 存在于 FileA$name 中并且存在于 File1$name 中,则使用 File1 中的所有列创建一个新的 CSV 表。因此,我将得到一个新的 CSV 文件,其中包含来自 File1 的 6 列和 190 个物种,因为它们与 FileA 匹配。