Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 无法将列表转换为数据帧_R_List_Dataframe - Fatal编程技术网

R 无法将列表转换为数据帧

R 无法将列表转换为数据帧,r,list,dataframe,R,List,Dataframe,我是R编程新手,我已经有好几天这个问题了。我从一个列表开始,我通过拆分文件创建。此列表在一行中包含多个项目 head(sales2) > $`7143443` >>[1] "SSS-U-CCXVCSS1" "L-CCX-8GETTS-LIC" >$`7208993` >>[1] "NFFGSR4=" "1MV-FT-1=" "VI-NT/TE=" >$`7241758` >>[1] "PW_SQSGG=" >$`9273

我是R编程新手,我已经有好几天这个问题了。我从一个列表开始,我通过拆分文件创建。此列表在一行中包含多个项目

head(sales2)
> $`7143443`
>>[1] "SSS-U-CCXVCSS1"   "L-CCX-8GETTS-LIC"

>$`7208993`
>>[1] "NFFGSR4="  "1MV-FT-1="  "VI-NT/TE=" 

>$`7241758`
>>[1] "PW_SQSGG="

>$`9273628`
>>[1] "O1941-SE9" "CCO887VA-K9"    "2901-SEC/K9" "CO1941-C/K9"

>$`9371709`
>>[1] "HGR__SASS=" "WWQTTB0S-L" "WS-RRRT48FP" "WTTTF24PS-L" 
[5] "GEDQTT8TS-L"  "WD-SRNS-2S-L" 

>$`9830473`
>>[1] "SPA$FFSB0S"
我想把它转换成数据帧,我用

x<-do.call(rbind, lapply(sales2,data.frame))
我要7143443的所有项目在一行,而不是在多行

通过这个,我想计算有多少行同时包含两个项目 例如“WS-C2960S-48TS-L”、“WS-C2960S-24TS-L”, 这两个元素在多少行中?
你也可以说这些元素在所有其他元素中的概率。

我不确定你最终想要的输出是什么。但是下面的脚本可以将列表转换为数据帧。也许您可以从这个数据框开始分析

# Create example list
sales2 <- list(`7143443` = c("SSS-U-CCXVCSS1", "L-CCX-8GETTS-LIC"),
            `7208993` = c("NFFGSR4=", "1MV-FT-1=", "VI-NT/TE="),
            `7241758` = "PW_SQSGG=",
            `9273628` = c("O1941-SE9", "CCO887VA-K9", "2901-SEC/K9", "CO1941-C/K9"),
            `9371709` = c("HGR__SASS=", "WWQTTB0S-L", "WS-RRRT48FP", "WTTTF24PS-L",
                          "GEDQTT8TS-L", "WD-SRNS-2S-L"),
            `9830473` = "SPA$FFSB0S")

# Load packages
library(dplyr)
library(purrr)

dat <- map(sales2, data_frame) %>%                    # Convert each list element to a data frame
  bind_rows(.id = "ID") %>%                           # Combine all data frame
  rename(Value = `.x[[i]]`) %>%                       # Change the name of the second column
  group_by(ID) %>%                                    # Group by the first column
  summarise(Value = paste0(Value, collapse = " "))    # Collapse the second column

dat
# A tibble: 6 × 2
       ID                                                                  Value
    <chr>                                                                  <chr>
1 7143443                                        SSS-U-CCXVCSS1 L-CCX-8GETTS-LIC
2 7208993                                           NFFGSR4= 1MV-FT-1= VI-NT/TE=
3 7241758                                                              PW_SQSGG=
4 9273628                          O1941-SE9 CCO887VA-K9 2901-SEC/K9 CO1941-C/K9
5 9371709 HGR__SASS= WWQTTB0S-L WS-RRRT48FP WTTTF24PS-L GEDQTT8TS-L WD-SRNS-2S-L
6 9830473                                                             SPA$FFSB0S

请给出一个可能的方法:但首先您必须将每个字符向量折叠为字符串。不过,这可能会让您开始,
do.call(rbind,lappy(sales2,toString))
这与@jogo所建议的非常相似。最终输出应该类似于Id 7143443产品“SSS-U-CCXVCSS1”,“L-CCX-8GETTS-LIC”
newdf-Hey@ycw,谢谢你的回答,但它给了我一个错误:参数
rename()
必须是无引号的变量名。参数
不是。你能帮我一下吗使用
bind_rows
后,你的第二列的列名是什么?你添加了``来括住第二列名称吗?哦,我没有用``括起来。谢谢你。另一个问题,当我使用
code
进行子集设置时**s2hello@ycw,非常感谢更新,但是我得到的dat2没有任何值。我正在使用id->9273628`=“O1941-SE9”,“CCO887VA-K9”,保存目标\u string1
# Create example list
sales2 <- list(`7143443` = c("SSS-U-CCXVCSS1", "L-CCX-8GETTS-LIC"),
            `7208993` = c("NFFGSR4=", "1MV-FT-1=", "VI-NT/TE="),
            `7241758` = "PW_SQSGG=",
            `9273628` = c("O1941-SE9", "CCO887VA-K9", "2901-SEC/K9", "CO1941-C/K9"),
            `9371709` = c("HGR__SASS=", "WWQTTB0S-L", "WS-RRRT48FP", "WTTTF24PS-L",
                          "GEDQTT8TS-L", "WD-SRNS-2S-L"),
            `9830473` = "SPA$FFSB0S")

# Load packages
library(dplyr)
library(purrr)

dat <- map(sales2, data_frame) %>%                    # Convert each list element to a data frame
  bind_rows(.id = "ID") %>%                           # Combine all data frame
  rename(Value = `.x[[i]]`) %>%                       # Change the name of the second column
  group_by(ID) %>%                                    # Group by the first column
  summarise(Value = paste0(Value, collapse = " "))    # Collapse the second column

dat
# A tibble: 6 × 2
       ID                                                                  Value
    <chr>                                                                  <chr>
1 7143443                                        SSS-U-CCXVCSS1 L-CCX-8GETTS-LIC
2 7208993                                           NFFGSR4= 1MV-FT-1= VI-NT/TE=
3 7241758                                                              PW_SQSGG=
4 9273628                          O1941-SE9 CCO887VA-K9 2901-SEC/K9 CO1941-C/K9
5 9371709 HGR__SASS= WWQTTB0S-L WS-RRRT48FP WTTTF24PS-L GEDQTT8TS-L WD-SRNS-2S-L
6 9830473                                                             SPA$FFSB0S
dat <- map(sales2, data_frame) %>%                    # Convert each list element to a data frame
  bind_rows(.id = "ID") %>%                           # Combine all data frame
  rename(Value = `.x[[i]]`)                           # Change the name of the second column

# After this, there is no need to collapse the rows

# Set the target string, User can change the strings here
target_string1 <- c("SSS-U-CCXVCSS1", "L-CCX-8GETTS-LIC")       

dat2 <- dat %>%
  filter(Value %in% target_string1) %>%               # Filter rows matching the targeted string
  distinct(ID, Value, .keep_all = TRUE) %>%           # Only keep one row if ID and Value have exact duplicated
  count(ID) %>%                                       # Count how many rows per ID
  filter(n > 1) %>%                                   # Keep only ID that the Count number is larger than 1
  select(ID)

dat2

# A tibble: 1 × 1
       ID
    <chr>
1 7143443