R 更改某些计数器值的列值

R 更改某些计数器值的列值,r,R,我正在使用如下循环读取数据: for(i in 1:2) { n= paste(i,".txt", sep="") a<- sprintf("table%d", i, i) data <- read.table(toString(n), header = TRUE, sep = "\t") ...... for(1:2中的i) { n=粘贴(i,“.txt”,sep=”“) a如果你想换东西,你需要暂时把它们放在某个地方 如果你想交换东西,你需要把它们暂时放在某个地方 如果你做了a

我正在使用如下循环读取数据:

for(i in 1:2)
{
n= paste(i,".txt", sep="")
a<- sprintf("table%d", i, i)
data <- read.table(toString(n), header = TRUE, sep = "\t")
......
for(1:2中的i)
{
n=粘贴(i,“.txt”,sep=”“)

a如果你想换东西,你需要暂时把它们放在某个地方


如果你想交换东西,你需要把它们暂时放在某个地方


如果你做了
a你所做的就是我在发现
plyr
之前处理事情的方式。下面是我现在处理类似情况的方式。可能有人可以向你展示更快的方法,但下面是我如何处理你的情况

library(plyr)

#Assuming all your files are in the working directory
filenames <- list.files(".", ".txt") 
#Assuming your files are "1.txt" etc
file.index <- sub("([0-9]+).txt", "\\1", filenames) 
#reads in all the files
import <- mdply(filenames, read.table, header = TRUE, sep = "\t") 
#Assigns the index to each group of rows from each file
import$index <- file.index[import$X1] 

#Gives you one big table to work with.
#Fix your reversing issue
import$Reqresponse.alt[import$Reqresponse == "a" & import$index %in% c(2,4,6,7,9,11,14,16,18,19,21,23,25,28,30,32,34,36)] <- "b"
import$Reqresponse.alt[import$Reqresponse == "b" & import$index %in% c(2,4,6,7,9,11,14,16,18,19,21,23,25,28,30,32,34,36)] <- "a"

import$Reqresponse <- import$Reqresponse.alt
import$Reqresponse.alt <- NULL

#Now your table should be clean
#You can use plyr to to summarise your data

import.summary <- ddply(import, .(index), summarise,
                        demo.mean = mean(demo), #functions you want to summarise with go here
                        demo.sd = sd(demo),
                        #etc
                        )
库(plyr)
#假设所有文件都在工作目录中

文件名你所做的只是在我发现
plyr
之前我是如何处理事情的。下面是我现在处理类似情况的方法。可能有人可以向你展示更快的方法,但下面是我如何处理你的情况

library(plyr)

#Assuming all your files are in the working directory
filenames <- list.files(".", ".txt") 
#Assuming your files are "1.txt" etc
file.index <- sub("([0-9]+).txt", "\\1", filenames) 
#reads in all the files
import <- mdply(filenames, read.table, header = TRUE, sep = "\t") 
#Assigns the index to each group of rows from each file
import$index <- file.index[import$X1] 

#Gives you one big table to work with.
#Fix your reversing issue
import$Reqresponse.alt[import$Reqresponse == "a" & import$index %in% c(2,4,6,7,9,11,14,16,18,19,21,23,25,28,30,32,34,36)] <- "b"
import$Reqresponse.alt[import$Reqresponse == "b" & import$index %in% c(2,4,6,7,9,11,14,16,18,19,21,23,25,28,30,32,34,36)] <- "a"

import$Reqresponse <- import$Reqresponse.alt
import$Reqresponse.alt <- NULL

#Now your table should be clean
#You can use plyr to to summarise your data

import.summary <- ddply(import, .(index), summarise,
                        demo.mean = mean(demo), #functions you want to summarise with go here
                        demo.sd = sd(demo),
                        #etc
                        )
库(plyr)
#假设所有文件都在工作目录中

文件名我不确定你的代码是否符合你的想法。我猜你只想对与你列出的数字匹配的I的值运行if语句?你想在%c(2,4,6,7,9,11,14,16,18,19,21,23,25,28,30,32,34,36)中使用类似
I%的内容吗
?@Dason:@sebastian-c这是我对他们想要什么的猜测。@Dason如果我不匹配,似乎会这样,所以我输入了相反的数字(我是智商低的survivin),但我想解决这个问题,我不确定你的代码是否与你认为的一样。我猜你只想对与你列出的数字匹配的I的值运行if语句?你想在%c(2,4,6,7,9,11,14,16,18,19,21,23,25,28,30,32,34,36)中使用类似于
I%的内容吗
?@Dason:@sebastian-c这是我对他们想要什么的猜测。@Dason如果我不匹配,似乎会这样,所以我输入了相反的数字(我是智商低的survivin)是的,我想确定当你把它们放在代码的同一行上时,你会很难阅读你的评论。你可以考虑把评论移到代码之前的线…谢谢你的提示,我将把它放在桌面上。当你把它们放在代码的同一行上时,我会仔细阅读你的评论。你可以考虑把评论移到TH。代码前的e行…谢谢你的提示,我会接受的