I am trying to use R to save the data to a specific format for another analysis software. For example, my data is:
Jane 2 3 0 1
Joe 2 0 1 1
John 1 0 0 0
Jack 2 1 3 1
Jay 0 0 0 0
I would like to save the data to a .txt file in the following format:
Jane 2301
Joe 2011
John 1000
Jack 2131
Jay 0000
If I used write.table(....., sep = "")
, there will be no space between the names and numbers. How can I save the file with space only between the name and numbers? Thank you very much!
Jane 2 3 0 1
Joe 2 0 1 1
John 1 0 0 0
Jack 2 1 3 1
Jay 0 0 0 0
d <- read.table(con<-file("clipboard"), header = F)
d1 <- data.frame(col1 = d$V1,
col2 = paste0(d$V2,d$V3,d$V4,d$V5))
write.table(d1, sep = " ", row.names = F)
You will need to change the data format in R before saving it. Here, I am using unite
from tidyr
to push all of those columns together (it ignores just V1
in this example, combining the rest)
Read sample data:
df <-
read.table(
text =
"
Jane 2 3 0 1
Joe 2 0 1 1
John 1 0 0 0
Jack 2 1 3 1
Jay 0 0 0 0"
)
Run unite
df %>%
unite(singleData, -V1, sep = "")
Returns:
V1 singleData
1 Jane 2301
2 Joe 2011
3 John 1000
4 Jack 2131
5 Jay 0000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.