how to create a variable name x1.outlier$id,... x11.outlier$id. these are available outside of loop ? if not how to do?

zstat <- qnorm(.975, mean = 0, sd = 1, lower.tail = TRUE)
cat("zstat=",zstat,"\n","\n")

for(i in 1:11) {
v <- paste("x",i,sep="")  
print(v)

data_outliers <- subset(zf, abs(df3[[v]]) > zstat)
numOutliers <- dim(data_outliers)[1] 
cat("Number of outliers is ", numOutliers, "\n")
cat("outlier ids are","\n")
print(data_outliers$id)

data_nooutliers <- subset(zf, abs(df3[[v]])< zstat)
numNooutliers <- dim(data_nooutliers)[1] 
cat(" ","\n")
cat("Number of nooutliers is ", numNooutliers, "\n")
cat("first 25 nooutlier ids are","\n")
print(head(data_nooutliers$id,25))

cat("\n","numOutliers+numNooutliers=",numOutliers+numNooutliers,"\n","\n")

#For each xi I want to create a file name xi.outliers, write that out to a csv file
#I will want to do a union join of these xi.outliers to find ids of outliers to be removed from zf
#I am not sure how to fix the error

string1 <- paste("x",i,sep="")
string2 <- ".outliers$id"
S <- paste(string1, string2, sep ="")
print(head(S,25))
S <- data_outliers$id
write.csv(S,"S.outliers.csv",row.names=none)
}

OUTPUT

[1] "x1"
Number of outliers is 347
outlier ids are
[1] 4 46 57 114 198 206 207 210 213 242 244 245 251 253 260 265 266 270 272 273
. . .
[341] 4472 4865 5859 6046 6386 6387 6447

Number of nooutliers is 6150
first 25 nooutlier ids are
[1] 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

numOutliers+numNooutliers= 6497

[1] "x1.outliers$id"
Error in eval(expr, p) : object 'none' not found

I think you want to do

string1 <- paste("x",i,".outliers$id.csv", sep="") #I'm not sure $ is allowed in file names. I would use and underscore
write.csv(data_outliers$id, string1, row.names=FALSE)

It might be better to store the values in a list rather than in csv files.

I am getting confused here.

my goal are outlier files with more specific names which include variable name:
x1.outliers, x2.outliers,...x11.outliers
thus I want to go from a more generic name data_outliers$id to
x1.outliers, x2.outliers,...x11.outliers

I can union join these to find rows of outliers to delete. I am having difficulty creating x1.outliers, ... x11.outliers variable names. I keep wanting to put them on the right hand side of an equality where in actuality they are on the left hand side. For example x1.outliers <- data_outliers$id. So string1 here is not what I want on left hand side but rather x1.outliers. I should forget about the csv file until I get that right!

To make a variable named x2.outliers, use the assign function.

i <- 2
string1 <- paste("x",i,".outliers", sep="")
string1
[1] "x2.outliers"
#make a variable
assign(string1, 1:6)
x2.outliers
[1] 1 2 3 4 5 6

Do we not need to populate string1 and call the new variable x1.outliers ? There is a confusion as to what is a dataframe and what is a variable in the code below. I tried to expand upon your reply where you used the assign function. This is not finished but open for comment.

zstat <- qnorm(.975, mean = 0, sd = 1, lower.tail = TRUE)
cat("zstat=",zstat,"\n","\n")

for(i in 1:11) {
v <- paste("x",i,sep="")  
print(v)

data_outliers <- subset(zf, abs(df3[[v]]) > zstat)
numOutliers <- dim(data_outliers)[1] 
cat("Number of outliers is ", numOutliers, "\n")
cat("outlier ids are","\n")
print(data_outliers$id)

data_nooutliers <- subset(zf, abs(df3[[v]])< zstat)
numNooutliers <- dim(data_nooutliers)[1] 
cat(" ","\n")
cat("Number of nooutliers is ", numNooutliers, "\n")
cat("first 25 nooutlier ids are","\n")
print(head(data_nooutliers$id,25))

cat("\n","numOutliers+numNooutliers=",numOutliers+numNooutliers,"\n","\n")

#create data frame with 11 empty vectors
combo <- data.frame(x1.outliers = double(),
                     x2.outliers=double(),
                     x3.outliers=double(),
                     x4.outliers=double(),
                     x5.outliers=double(),
                     x6.outliers=double(),
                     x7.outliers=double(),
                     x8.outliers=double(),
                     x9.outliers=double(),
                     x10.outliers=double(),
                     x11.outliers=double()
                     )

#view structure of the data frame
str(combo)

#make a variable name
string1 <- paste("x",i,".outliers", sep="")
string1  # "xi.outliers"

#make a variable (populate the variable name)
class(assign(string1, data_outliers)) #data.frame

#create a data frame of the new  outliers (length of outliers will be different)
combo <- rbind(x1.outliers,x2.outliers,x3.outliers...x11.outliers)
print(head(combo,10))
class(combo) #data.frame
temp1 <- sort(combo) #undefined columns selected in data.frame combo
nrow(temp1)
head(temp1,10)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.