Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
存储在For循环(r)中的动态变量名_R_Dataframe_For Loop_Variables - Fatal编程技术网

存储在For循环(r)中的动态变量名

存储在For循环(r)中的动态变量名,r,dataframe,for-loop,variables,R,Dataframe,For Loop,Variables,我试图使用一个循环来操作多个文件,但也保留每个循环周期中变量/输出的名称 下面是我正在尝试做的一个例子: #define my source SourceDir <- file.path('where/it/is') #define my array Wpns <- ('Bren', 'Welrod', 'Vickers') #start for-loop for (Wpn in Wpns){ #example of for loop contents 1 (is th

我试图使用一个循环来操作多个文件,但也保留每个循环周期中变量/输出的名称

下面是我正在尝试做的一个例子:

#define my source
SourceDir <- file.path('where/it/is')

#define my array
Wpns <- ('Bren', 'Welrod', 'Vickers')

#start for-loop
for (Wpn in Wpns){

    #example of for loop contents 1 (is this a sensible way to get a file path?)
    WpnFile <- file.path(paste0(SourceDir, '/My'_, Wpn, '_File.txt')

    #example of for loop contents 2
    WpnDataFrame <- read_delim(WpnFile, ' ')

}
这是我第一次使用r,所以如果我写的东西(或者我试图实现的东西)看起来很奇怪,我也不会感到惊讶


(对于一些重要的上下文:我正在尝试操作文本文件中的数据,然后绘制数据。但是,我确实需要检查For循环的每个阶段(例如,我创建的数据帧),这样我可以检查我正在做什么。这也是帮助我可视化我正在做的更改的关键,因为我不习惯使用矩阵,而且我以后可能需要返回特定的数据帧。)

因此,如果我做对了,您希望将文件名添加到循环中的源中,然后加载相应的数据。如果是的话,我可以建议这样做

# define my source
SourceDir <- file.path('where/it/is') # unchanged

#define my array
Wpns <- c('Bren', 'Welrod', 'Vickers') # you must use c()

# creating an empty list for your data being loaded
WpnDataFrame <- mylist <- vector("list", length(Wpns))
# naming the list
names(WpnDataFrame) <- Wpns

# creating a vector for the file paths
WpnFile <- rep(NA, length(Wpns))

# i can not show it with real data since my pc has other file paths then yours so here is some data for demonstration
df <- 1:3

# running for loop
for(i in 1:length(Wpns)){
# saving each file path
WpnFile[i] <- file.path(paste0(SourceDir, '/My_', Wpns[i], '_File.txt'))

# loading the data
# for demonstration:  
WpnDataFrame[[Wpns[i]]] <- df[i] 
# you want to do this: 
# WpnDataFrame[[Wpns[i]]] <- read_delim(WpnFile, ' ')
}

注意:我使用了WonDataFrame的类列表,因为我不确定加载的数据是否为相同的格式(相同的列数,相同的列顺序)。在列表中,一个df是否有3列,另一个df是否有4列并不重要。

如果是重复的,我道歉,我在发帖前会仔细检查,但我不太擅长用准确的术语来描述我的目标和意图,而我缺乏经验意味着我忽略了有时可能对我有帮助的答案。没问题。在标记可能的副本时,注释是一条自动消息。看看链接中的答案是否对你有帮助。如果没有,请重写您的问题,突出显示您的问题与链接的不同之处。@SolebaySharp:另请参见我想说的这实际上是:您正在询问如何使用
for
循环导入文件,但是,即使是最漂亮的代码也无法做到这一点,因为您错误地定义了向量:
Wpns,因此,从外观上看,数据帧列表是一种方法。但是我现在有一个4×1数据帧的列表,其中第一个有一条路径,后面跟着三个NA,第二个有两条路径,后面跟着两个NA,依此类推。“mylist”中也充满了NULL,它在做什么?忽略这一点,当我复制粘贴它时,我错误地删除了一点,并用错误的信息重新填充了它。非常感谢您的帮助,虽然我仍然对我的空名单感到困惑,但这一点都不重要!例如,您可以尝试为i插入一些值,然后遍历for循环中的代码,看看会发生什么。执行此操作时,检查每个对象是否为您期望的对象。
# define my source
SourceDir <- file.path('where/it/is') # unchanged

#define my array
Wpns <- c('Bren', 'Welrod', 'Vickers') # you must use c()

# creating an empty list for your data being loaded
WpnDataFrame <- mylist <- vector("list", length(Wpns))
# naming the list
names(WpnDataFrame) <- Wpns

# creating a vector for the file paths
WpnFile <- rep(NA, length(Wpns))

# i can not show it with real data since my pc has other file paths then yours so here is some data for demonstration
df <- 1:3

# running for loop
for(i in 1:length(Wpns)){
# saving each file path
WpnFile[i] <- file.path(paste0(SourceDir, '/My_', Wpns[i], '_File.txt'))

# loading the data
# for demonstration:  
WpnDataFrame[[Wpns[i]]] <- df[i] 
# you want to do this: 
# WpnDataFrame[[Wpns[i]]] <- read_delim(WpnFile, ' ')
}
WpnFile
[1] "where/it/is/My_Bren_File.txt"    
"where/it/is/My_Welrod_File.txt" 
[3] "where/it/is/My_Vickers_File.txt"

WpnDataFrame
$Bren
[1] 1
$Welrod
[1] 2
$Vickers
[1] 3