s.s.m. fauzi
2012-Sep-26 06:43 UTC
[R] Ask for help - how to change WHIRR.117.csv to WHIRR_117.csv
Hi,
I have a script below.
dat <- read.table(file="pt.csv", header=T, sep=",",
row.names=1,
col.names=1)
dat
for(which_col in seq_len(ncol(dat)))
{
subset_data <- dat[,which_col:ncol(dat)]
file_name <- sprintf('%s.csv', colnames(dat)[which_col])
write.csv(subset_data, file_name)
message(sprintf('Saving %s', file_name))
}
dput(head(dat))
structure(list(WHIRR.25 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.28 = c(0L,
0L, 1L, 0L, 0L, 0L), WHIRR.55 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.61 = c(0L,
0L, 1L, 0L, 0L, 0L), WHIRR.76 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.87 = c(0L,
0L, 0L, 0L, 0L, 0L), WHIRR.92 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.115 c(0L,
0L, 0L, 0L, 0L, 0L), WHIRR.117 = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(
"WHIRR.25",
"WHIRR.28", "WHIRR.55", "WHIRR.61",
"WHIRR.76", "WHIRR.87", "WHIRR.92",
"WHIRR.115", "WHIRR.117"), row.names = c("Adrian
Cole", "Alison Wong",
"Andrei Savu", "Bruno Dumon", "Edward J. Yoon",
"Eugene Koontz"
), class = "data.frame")
The script is able to save the file in the directory, but with the
following name:
WHIRR.25.csv
WHIRR.28.csv
WHIRR.55.csv
WHIRR.61.csv
WHIRR.76.csv
WHIRR.87.csv
WHIRR.92.csv
WHIRR.115.csv
WHIRR.117.csv
My first question is:
How can I change or convert the name above to WHIRR_25.csv, WHIRR_28.csv,
WHIRR_55.csv, etc?
My second question is:
The last column which is WHIRR.177.csv is not properly created, the output
for the last column is as below:
X1
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 1
15 0
Suppose, the last column should be properly created like this, as below:
WHIRR.117
Adrian Cole 0
Alison Wong 0
Andrei Savu 0
Bruno Dumon 0
Edward J. Yoon 0
Eugene Koontz 0
Jakob Homan 0
Kelvin Kakugawa 0
Kirk True 0
Lars George 0
Soren Macbeth 0
Stu Hood 0
Tibor Kiss 0
Tom White 1
Unassigned 0
Appreciate your thought...
[[alternative HTML version deleted]]
Rui Barradas
2012-Sep-26 10:26 UTC
[R] Ask for help - how to change WHIRR.117.csv to WHIRR_117.csv
Hello,
Maybe the code below answers to both your questions (Q1 and Q2)
cnames <- colnames(dat)
cnames <- sub("WHIRR\\.", "WHIRR_", cnames)
for(which_col in seq_len(ncol(dat)))
{
subset_data <- dat[which_col:ncol(dat)] # change 1, Q2
file_name <- sprintf('%s.csv', cnames[which_col]) # change 2,
Q1
#write.csv(subset_data, file_name) # debug 1
message(sprintf('Saving %s', file_name))
if(which_col == ncol(dat)) print(subset_data) # debug 2
}
Em 26-09-2012 07:43, s.s.m. fauzi escreveu:> Hi,
> I have a script below.
>
> dat <- read.table(file="pt.csv", header=T, sep=",",
row.names=1,
> col.names=1)
> dat
> for(which_col in seq_len(ncol(dat)))
> {
> subset_data <- dat[,which_col:ncol(dat)]
> file_name <- sprintf('%s.csv', colnames(dat)[which_col])
> write.csv(subset_data, file_name)
> message(sprintf('Saving %s', file_name))
> }
>
> dput(head(dat))
>
> structure(list(WHIRR.25 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.28 = c(0L,
> 0L, 1L, 0L, 0L, 0L), WHIRR.55 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.61 = c(0L,
> 0L, 1L, 0L, 0L, 0L), WHIRR.76 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.87 = c(0L,
> 0L, 0L, 0L, 0L, 0L), WHIRR.92 = c(0L, 0L, 0L, 0L, 0L, 0L), WHIRR.115 >
c(0L,
> 0L, 0L, 0L, 0L, 0L), WHIRR.117 = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(
> "WHIRR.25",
> "WHIRR.28", "WHIRR.55", "WHIRR.61",
"WHIRR.76", "WHIRR.87", "WHIRR.92",
> "WHIRR.115", "WHIRR.117"), row.names = c("Adrian
Cole", "Alison Wong",
> "Andrei Savu", "Bruno Dumon", "Edward J.
Yoon", "Eugene Koontz"
> ), class = "data.frame")
>
> The script is able to save the file in the directory, but with the
> following name:
> WHIRR.25.csv
> WHIRR.28.csv
> WHIRR.55.csv
> WHIRR.61.csv
> WHIRR.76.csv
> WHIRR.87.csv
> WHIRR.92.csv
> WHIRR.115.csv
> WHIRR.117.csv
>
> My first question is:
> How can I change or convert the name above to WHIRR_25.csv, WHIRR_28.csv,
> WHIRR_55.csv, etc?
>
> My second question is:
> The last column which is WHIRR.177.csv is not properly created, the output
> for the last column is as below:
>
> X1
> 1 0
> 2 0
> 3 0
> 4 0
> 5 0
> 6 0
> 7 0
> 8 0
> 9 0
> 10 0
> 11 0
> 12 0
> 13 0
> 14 1
> 15 0
>
> Suppose, the last column should be properly created like this, as below:
>
> WHIRR.117
> Adrian Cole 0
> Alison Wong 0
> Andrei Savu 0
> Bruno Dumon 0
> Edward J. Yoon 0
> Eugene Koontz 0
> Jakob Homan 0
> Kelvin Kakugawa 0
> Kirk True 0
> Lars George 0
> Soren Macbeth 0
> Stu Hood 0
> Tibor Kiss 0
> Tom White 1
> Unassigned 0
>
> Appreciate your thought...
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.