将R中的数组分割成数据框。

7

我对R语言非常陌生,不知道如何表达我的问题,我在这里发现了一个类似的问题: 如何将字符向量拆分为数据框?但是答案中的长度已经固定好了,我无法用它来解决我的问题。

我在R中有一些数组数据。

TEST <- c("Value01:100|Value02:200|Value03:300|","Value04:1|Value05:2|",
            "StillAValueButNamesAreNotConsistent:12345.6789|",
              "AlsoNotAllLinesAreTheSameLength:1|")

数据以成对的形式存储,我想将其拆分为数据框,如下所示:
Variable Value
Value01    100
Value02    200
Value03    300
Value04    1
Value05    2
StillAValueButNamesAreNotConsistent   12345.6789
AlsoNotAllLinesAreTheSameLength     1

变量名称是一个字符串,其值始终是一个数字。
如有需要,我们会提供帮助!
谢谢。
4个回答

5

您可以使用基于 tidyr 的解决方案。将向量 TEST 转换为数据帧,并从每行中删除最后一个 |,因为它本身没有任何含义。

现在,使用 tidyr::separate_rows 基于 | 展开行,然后使用 tidyr::separate 函数将数据分成两列。

library(dplyr)
library(tidyr)

data.frame(TEST) %>%
  mutate(TEST = gsub("\\|$","",TEST)) %>%
  separate_rows(TEST, sep = "[|]") %>%
  separate(TEST, c("Variable", "Value"), ":")

#                              Variable      Value
# 1                             Value01        100
# 2                             Value02        200
# 3                             Value03        300
# 4                             Value04          1
# 5                             Value05          2
# 6 StillAValueButNamesAreNotConsistent 12345.6789
# 7     AlsoNotAllLinesAreTheSameLength          1

5
我们可以在基础R中用一行代码实现。只需将|字符更改为换行符,然后在read.table()中使用:作为sep值即可。您还可以在那里设置列名。
read.table(text = gsub("\\|", "\n", TEST), sep = ":", 
    col.names = c("Variable", "Value"))

#                              Variable    Value
# 1                             Value01   100.00
# 2                             Value02   200.00
# 3                             Value03   300.00
# 4                             Value04     1.00
# 5                             Value05     2.00
# 6 StillAValueButNamesAreNotConsistent 12345.68
# 7     AlsoNotAllLinesAreTheSameLength     1.00

0

使用基本R语言:

(我已经将每个步骤分解,希望能使代码更清晰)

# your data
myvec <- c("Value01:100|Value02:200|Value03:300|","Value04:1|Value05:2|",
           "StillAValueButNamesAreNotConsistent:12345.6789|",
           "AlsoNotAllLinesAreTheSameLength:1|")

# convert into one long string
all_text_str <- paste0(myvec, collapse="")

# split the string by "|"
all_text_vec <- unlist(strsplit(all_text_str, split="\\|"))

# split each "|"-group by ":"
data_as_list <- strsplit(all_text_vec, split=":")

# collect into a dataframe
df <- do.call(rbind, data_as_list)

# clean up the dataframe by adding names and converting value to numeric
names(df) <- c("variable", "value")
df$value <- as.numeric(df$value)

0

通过使用 strsplitunlist 函数。每个命令的输出如下所示。

输入

 TEST
 # [1] "Value01:100|Value02:200|Value03:300|"           
 # [2] "Value04:1|Value05:2|"                           
 # [3] "StillAValueButNamesAreNotConsistent:12345.6789|"
 # [4] "AlsoNotAllLinesAreTheSameLength:1|"             

| 分割,然后按 : 分割

 my_list <- strsplit(unlist(strsplit(TEST, "|", fixed = TRUE)), ":", fixed = TRUE)
 my_list
 # [[1]]
 # [1] "Value01" "100"    

 # [[2]]
 # [1] "Value02" "200"    

 # [[3]]
 # [1] "Value03" "300"    

 # [[4]]
 # [1] "Value04" "1"      

 # [[5]]
 # [1] "Value05" "2"      

 # [[6]]
 # [1] "StillAValueButNamesAreNotConsistent" "12345.6789"                         

 # [[7]]
 # [1] "AlsoNotAllLinesAreTheSameLength" "1"                              

将上述列表转换为数据框

 df <- data.frame(matrix(unlist(my_list), ncol = 2, byrow=TRUE))
 df
 #                                    X1         X2
 # 1                             Value01        100
 # 2                             Value02        200
 # 3                             Value03        300
 # 4                             Value04          1
 # 5                             Value05          2
 # 6 StillAValueButNamesAreNotConsistent 12345.6789
 # 7     AlsoNotAllLinesAreTheSameLength          1

将列名添加到数据框中

 names(df) <- c("Variable", "Value")
 df
 #                              Variable      Value
 # 1                             Value01        100
 # 2                             Value02        200
 # 3                             Value03        300
 # 4                             Value04          1
 # 5                             Value05          2
 # 6 StillAValueButNamesAreNotConsistent 12345.6789
 # 7     AlsoNotAllLinesAreTheSameLength          1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接