write.table

rename filename with a data column

假装没事ソ 提交于 2019-12-11 20:19:19
问题 I hope this question is clear and feasible. I have many csv files labelled with a simple numeric ID (e.g., 1.csv, 2.csv, 3.csv, etc.). Another csv file contains the actual data like below ID Site Location Block 1 L1 1 a 2 L2 2 b 3 etc My question is how to link the ID in the data file to rename the csv files. The output I would ideally like to have is L11a.csv, L22b.csv, etc. I think I need to concatenate each row observation's Site, Location, and Block into another column and then use that

Save each element of a list to text file

梦想的初衷 提交于 2019-12-11 08:30:57
问题 I have a folder with many csv files. I read them like this files <- list.files(pattern = "*.csv") [1] "ETo_1980.csv" "ETo_1981.csv" "ETo_1982.csv" "ETo_1983.csv" "ETo_1984.csv" "ETo_1985.csv" [7] "ETo_1986.csv" "ETo_1987.csv" "ETo_1988.csv" "ETo_1989.csv" "ETo_1990.csv" "ETo_1991.csv" [13] "ETo_1992.csv" "ETo_1993.csv" "ETo_1994.csv" "ETo_1995.csv" "ETo_1996.csv" "ETo_1997.csv" [19] "ETo_1998.csv" "ETo_1999.csv" "ETo_2000.csv" "ETo_2001.csv" "ETo_2002.csv" "ETo_2003.csv" [25] "ETo_2004.csv"

How to prevent write.csv from changing POSIXct, dates and times class back to character/factors?

大憨熊 提交于 2019-12-09 01:23:48
问题 I have a .csv file with one field each for datetime, date and time. Originally they are all character fields and I have converted them accordingly. At the end of my code, if I do: str(data) I will get datetime: POSIXct date: Date time: Class 'times' atomic [1:2820392] (....) attr(*, "format")= chr "h:m:s" Now, I am very happy with this and I want to create a .csv file, so this is what I have: write.csv(data, file = "data.csv", row.names = FALSE) I have also tried write.table(data, "data.csv",

Controlling number of decimal places in write.table while maintaining class “numeric”

有些话、适合烂在心里 提交于 2019-12-06 04:46:55
Generally speaking, controlling the number of digits to display when calling write.table is straightforward. Given the excellent question and answer seen here , we see that format is the means to accomplish this feat. However, this creates a file that when viewed by a text editor, shows quotes around numeric values. This can be turned off by setting quote = FALSE in write.table . Unfortunately, this has the side effect of not putting quotations around character columns. Observe: Normal Case (without format and standard output behavior) df <- data.frame(c1 = 1:5/100, c2 = LETTERS[1:5]) write

Unimplemented type list when trying to write.table

本秂侑毒 提交于 2019-12-03 05:36:26
问题 I have the following data.table (data.frame) called output: > head(output) Id Title IsProhibited 1 10000074 Renault Logan, 2005 0 2 10000124 Ñêëàäñêîå ïîìåùåíèå, 345 ì<U+00B2> 0 3 10000175 Ñó-øåô 0 4 10000196 3-ê êâàðòèðà, 64 ì<U+00B2>, 3/5 ýò. 0 5 10000387 Samsung galaxy S4 mini GT-I9190 (÷¸ðíûé) 0 6 10000395 Êàðòèíà ""Êðûì. Ïîñåëîê Àðîìàò"" (õîëñò, ìàñëî) 0 I am trying to export it to a CSV like so: > write.table(output, 'output.csv', sep = ',', row.names = FALSE, append = T) However, when

Unimplemented type list when trying to write.table

别等时光非礼了梦想. 提交于 2019-12-02 18:56:23
I have the following data.table (data.frame) called output: > head(output) Id Title IsProhibited 1 10000074 Renault Logan, 2005 0 2 10000124 Ñêëàäñêîå ïîìåùåíèå, 345 ì<U+00B2> 0 3 10000175 Ñó-øåô 0 4 10000196 3-ê êâàðòèðà, 64 ì<U+00B2>, 3/5 ýò. 0 5 10000387 Samsung galaxy S4 mini GT-I9190 (÷¸ðíûé) 0 6 10000395 Êàðòèíà ""Êðûì. Ïîñåëîê Àðîìàò"" (õîëñò, ìàñëî) 0 I am trying to export it to a CSV like so: > write.table(output, 'output.csv', sep = ',', row.names = FALSE, append = T) However, when doing so I get the following error: Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep,

Replace all values in a row with 0 if one of the variables has missing data

无人久伴 提交于 2019-12-02 08:33:53
问题 As the title states I'm attempting to replace all values in a given row with 0 if any variable has NA and output this using write.table. For example: x y z 2 3 5 4 NA 1 3 2 1 to x y z 2 3 5 0 0 0 3 2 1 At the moment my code looks like the following myvars <- c("x", "y", "z") newdata <- mydata[myvars] write.table(newdata, "data.txt", col.names=FALSE, row.names=FALSE) 回答1: complete.cases seems appropriate here: dat[!complete.cases(dat),] <- 0 dat # x y z #1 2 3 5 #2 0 0 0 #3 3 2 1 回答2: While

Replace all values in a row with 0 if one of the variables has missing data

南笙酒味 提交于 2019-12-02 03:55:29
As the title states I'm attempting to replace all values in a given row with 0 if any variable has NA and output this using write.table. For example: x y z 2 3 5 4 NA 1 3 2 1 to x y z 2 3 5 0 0 0 3 2 1 At the moment my code looks like the following myvars <- c("x", "y", "z") newdata <- mydata[myvars] write.table(newdata, "data.txt", col.names=FALSE, row.names=FALSE) complete.cases seems appropriate here: dat[!complete.cases(dat),] <- 0 dat # x y z #1 2 3 5 #2 0 0 0 #3 3 2 1 While the answer by @thelatemail is really cool, It won't hurt to learn one more trick. Here is my solution: dat[rowSums

How to prevent write.csv from changing POSIXct, dates and times class back to character/factors?

妖精的绣舞 提交于 2019-12-01 01:18:52
I have a .csv file with one field each for datetime, date and time. Originally they are all character fields and I have converted them accordingly. At the end of my code, if I do: str(data) I will get datetime: POSIXct date: Date time: Class 'times' atomic [1:2820392] (....) attr(*, "format")= chr "h:m:s" Now, I am very happy with this and I want to create a .csv file, so this is what I have: write.csv(data, file = "data.csv", row.names = FALSE) I have also tried write.table(data, "data.csv", sep = ",", row.names = FALSE) And I get the same result with both, which is all my convertion gets

write.table writes unwanted leading empty column to header when has rownames

混江龙づ霸主 提交于 2019-11-28 02:53:35
check this example: > a = matrix(1:9, nrow = 3, ncol = 3, dimnames = list(LETTERS[1:3], LETTERS[1:3])) > a A B C A 1 4 7 B 2 5 8 C 3 6 9 the table displays correctly. There are two different ways of writing it to file... write.csv(a, 'a.csv') which gives as expected: "","A","B","C" "A",1,4,7 "B",2,5,8 "C",3,6,9 and write.table(a, 'a.txt') which screws up "A" "B" "C" "A" 1 4 7 "B" 2 5 8 "C" 3 6 9 indeed, an empty tab is missing.... which is a pain in the butt for downstream things. Is this a bug or a feature? Is there a workaround? (other than write.table(cbind(rownames(a), a), 'a.txt', row