Reputation: 648
how to convert a text file given in below format
ASDDSDAS 111 132 456
DSSADSDA 1 12335
Into a dataframe with
circleid nodeid
ASDDSDAS 111
ASDDSDAS 132
ASDDSDAS 456
DSSADSDA 1
DSSADSDA 12335
So far
library(tidyverse)
circ <- read_file(circ_file)
cir <- read.csv(text=circ, sep=" ", header = FALSE) %>%
separate_rows(circ, convert = TRUE) %>%
filter(!is.na(circ))
#Error: Unknown column ASDDSDAS.
Upvotes: 1
Views: 82
Reputation: 76402
Here is a way using base R only.
fun <- function(x){
data.frame(circleid = x[1], nodeid = as.numeric(x[-1]), stringsAsFactors = FALSE)
}
uri <- "https://raw.githubusercontent.com/pranavn91/APS/master/100129275726588145876.circles"
txt <- readLines(uri)
result <- do.call(rbind, lapply(strsplit(txt, "[[:blank:]]+"), fun))
print(head(result), digits = 20)
# circleid nodeid
#1 Az1VWXrXzeg 111439704478822924298
#2 Az1VWXrXzeg 102604554618567114752
#3 Az1VWXrXzeg 113609548050074206218
#4 Az1VWXrXzeg 103090889232468295680
#5 Az1VWXrXzeg 108271734388645117952
#6 Az1VWXrXzeg 111566503164646162432
Upvotes: 2
Reputation: 1871
here is a way using the tidyverse
library(tidyverse)
cir <-
read_table2("https://raw.githubusercontent.com/pranavn91/APS/master/100129275726588145876.circles", col_names = FALSE) %>%
rename(circleid = X1) %>%
gather(key = "key", value = "nodeid", -circleid) %>%
arrange(circleid, nodeid) %>%
select(-key)
This does generate a warning message:
Warning message:
In rbind(names(probs), probs_f) :
number of columns of result is not a multiple of vector length (arg 2)
Upvotes: 2