1

例如,我有一个数据框,

X1 12:37  
X2  3,0,0

我想将 3 0 和 0 分别提取到一个新的数据框中。

不知道我该怎么做?

提前致谢...

4

3 回答 3

2

You can start by reading your Lines ans splitting them to extract Number from row names.

ll <- readLines(textConnection('X1 12:37  
X2  3,0,0'))
res <- strsplit(ll,' ')
[[1]]
[1] "X1"    "12:37" ""     

[[2]]
[1] "X2"    ""      "3,0,0"

Or using read.table:

res1 <- read.table(text='X1 12:37  
X2  3,0,0')
  V1    V2
1 X1 12:37
2 X2 3,0,0

Than you can access the elements containing comma for example, and apply strsplit again as mentioned by the other solution.

unlist(res)[grep(',',unlist(res))]
 "3,0,0"
于 2013-06-27T09:11:25.970 回答
1

You can get the numbers out using strsplit:

strsplit("3,0,0", ",")

It's not entirely clear if your data is actually in a data frame, or just a vector (people new to R have a tendency to call everything containing data as a "data frame"), but hopefully you can see how to apply the function.

于 2013-06-27T09:10:24.393 回答
1

使用@agstudy 的“res1”:

res1 <- read.table(
  text='X1 12:37
  X2  3,0,0')
temp <- read.table(text = as.character(res1$V2), 
                   sep = ",", header = FALSE, fill = TRUE)
temp
#      V1 V2 V3
# 1 12:37 NA NA
# 2     3  0  0
temp[complete.cases(temp), ]
#   V1 V2 V3
# 2  3  0  0
于 2013-06-27T09:34:58.270 回答