Reputation: 87
[1] genos IDV_V 24 0.506472 14.0206 1.17 0 P [2] Lcrop IDV_V 6 0.768434E-06 0.212724E-04 0.00 0 B [3] Lgenos IDV_V 24 0.768434E-06 0.212724E-04 0.00 0 B [4] Residual SCA_V 160 1.00000 27.6828 8.83 0 P
Hey, I have a text and would like to convert it into a data frame (in total 7 columns), how should I do it? Hope to hear from you!
Upvotes: 0
Views: 38
Reputation: 2140
Are you looking for something like this:
# make a vector of the text data
mytext <- c("[1] genos IDV_V 24 0.506472 14.0206 1.17 0 P",
"[2] Lcrop IDV_V 6 0.768434E-06 0.212724E-04 0.00 0 B",
"[3] Lgenos IDV_V 24 0.768434E-06 0.212724E-04 0.00 0 B",
"[4] Residual SCA_V 160 1.00000 27.6828 8.83 0 P")
# Converting text into a data frame
df <- read.table(text = mytext, sep = " ", col.names = c("id", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"))
df[,c(1,8)]<-NULL
df
v1 v2 v3 v4 v5 v6 v8
1 genos IDV_V 24 0.506472000000 14.0206000000 1.17 P
2 Lcrop IDV_V 6 0.000000768434 0.0000212724 0.00 B
3 Lgenos IDV_V 24 0.000000768434 0.0000212724 0.00 B
4 Residual SCA_V 160 1.000000000000 27.6828000000 8.83 P
Upvotes: 0
Reputation: 24742
You could use readr::read_table()
like this:
library(readr)
library(dplyr)
read_table(data, col_names=F) %>% mutate(X1=paste(X1,X2)) %>% select(-X2)
Output:
# A tibble: 4 × 7
X1 X3 X4 X5 X6 X7 X8
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 genos IDV_V 24 0.506 14.0 1.17 0 P
2 Lcrop IDV_V 6 0.000000768 0.0000213 0 0 B
3 Lgenos IDV_V 24 0.000000768 0.0000213 0 0 B
4 Residual SCA_V 160 1 27.7 8.83 0 P
Input:
data = c("genos IDV_V 24 0.506472 14.0206 1.17 0 P", "Lcrop IDV_V 6 0.768434E-06 0.212724E-04 0.00 0 B",
"Lgenos IDV_V 24 0.768434E-06 0.212724E-04 0.00 0 B", "Residual SCA_V 160 1.00000 27.6828 8.83 0 P"
)
Upvotes: 1