【问题标题】:How to split colums into multiple colums and find frequency in R?如何将列拆分为多列并在 R 中查找频率?
【发布时间】:2020-03-06 22:27:17
【问题描述】:

ODK 响应

1
1; 2
1; 2; 3
1; 2; 3; 5
1; 2; 4
1; 2; 4; 5; 6
1; 2; 4; 6
1; 2; 4; 7

1 is Crop failure-
2 is Water shortage
3 is Land degradation
4 is Lack of HH Labor
5 is Lack of income from agriculture
6 is Lack of manure / fertilizer
7 is Others

我想要一张这样的桌子

Crop failure- 8
Water shortage- 7
Land degradation- 6
Lack of HH Labor- 1
Lack of income from agriculture- 2
Lack of manure / fertilizer- 2
Others- 1

我曾尝试在 R 中使用 dplyr 将具有多个值的单列拆分为多个列,但无法提供帮助。

【问题讨论】:

  • 为什么是Land degradation- 6 Lack of HH Labor- 1?它应该分别是 2 和 4。

标签: r sorting aggregation


【解决方案1】:

使用plyr,可以获得以下内容:

Condition = c("Crop failure", "Water shortage", "Lang degradation", "Lack of HH Labor",  "Lack of income from agriculture", "Lack of manure / fertilizer", "Others")
Type = c(1:7)
df = data.frame(Condition, Type)

vector = c(1,1,2,1,2,3,1,2,3,5,1,2,4,1,2,4,5,6,1,2,4,6,1,2,4,7)
t = plyr::count(vector)
colnames(t) = c("Type","Freq")

df =merge(df,t)

你会得到:

> df
  Type                       Condition Freq
1    1                    Crop failure    8
2    2                  Water shortage    7
3    3                Lang degradation    2
4    4                Lack of HH Labor    4
5    5 Lack of income from agriculture    2
6    6     Lack of manure / fertilizer    2
7    7                          Others    1

【讨论】:

  • 单个单元格中有多个变量,例如单元格一只有 1,单元格二有 1;2,单元格 3 有 1;2;3,等等。单个单元格中的所有变量都是由 ; 分隔在 .csv 文件中,单列中有 32 行具有多个值。我的尝试是得到所有 1,2,3 等的总和。
【解决方案2】:
  • 如果您想使用base R,以下解决方案可能会对您有所帮助。

假设您的输入是

response <- c(1, 1, 2, 1, 2, 3, 1, 2, 3, 5, 1, 2, 4, 1, 2, 4, 5, 6, 1, 2, 4, 6, 1, 2, 4, 7)

然后

status <- c("Crop failure", "Water shortage", "Lang degradation", "Lack of HH Labor",  "Lack of income from agriculture", "Lack of manure / fertilizer", "Others")
df <- as.data.frame(table(factor(response,labels = status),dnn = list("Status")))

可以给你这样的输出

> df
                           Status Freq
1                    Crop failure    8
2                  Water shortage    7
3                Lang degradation    2
4                Lack of HH Labor    4
5 Lack of income from agriculture    2
6     Lack of manure / fertilizer    2
7                          Others    1
  • 如果你想有一个明细表: 假设您的输入是:
r <- list(1, c(1, 2), c(1, 2, 3), c(1, 2, 3, 5), c(1, 2, 4), c(1, 
2, 4, 5, 6), c(1, 2, 4), 6, c(1, 2, 4, 7))
type = seq(1,7)
dt <- as.data.frame(t(sapply(r, function(v) sapply(type, function(k) sum(k==v)))))
colnames(M) <- paste0("type",type)

给了

> dt
  type1 type2 type3 type4 type5 type6 type7
1     1     0     0     0     0     0     0
2     1     1     0     0     0     0     0
3     1     1     1     0     0     0     0
4     1     1     1     0     1     0     0
5     1     1     0     1     0     0     0
6     1     1     0     1     1     1     0
7     1     1     0     1     0     0     0
8     0     0     0     0     0     1     0
9     1     1     0     1     0     0     1

此外,每个类型条目的总和可以通过colSums计算:

> colSums(dt)
type1 type2 type3 type4 type5 type6 type7 
    8     7     2     4     2     2     1

或者你可以使用match(),即,

dt <- as.data.frame(t(sapply(r, function(v) !is.na(match(type,v)))))
> dt
  type1 type2 type3 type4 type5 type6 type7
1  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
2  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
3  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
4  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE
5  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
6  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
7  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
8 FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
9  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE

【讨论】:

  • 我的数据存储在 .csv 文件中。 1个; 2 1; 2; 3 1; 2; 3; 5 1; 2; 4 1; 2; 4; 5个; 6 1; 2; 4; 6 1; 2; 4; 7 1; 2; 5 1; 2; 5个; 6 1; 2; 5个; 7 1; 2; 6 1; 2; 7 1; 3 1; 3; 7 1; 4 1; 5 1; 5个; 6 1; 6 2 2; 3 2; 3; 5 2; 3; 6 2; 4; 6 2; 5 2; 5个; 6 2; 5个; 7 2; 6 2; 7 5 5; 7 7 想计算并得到我前面提到的所有 1(作物歉收)、2(缺水)、3(Lang 退化)等的总和。
  • 您的意思是要从 .csv 文件中读取数字吗?或者你想要什么? @SaneeshCS 我建议您在问题帖子中提供一些代码或图片中的玩具示例,这样其他人会更容易帮助您
  • 我想统计一列中多行的变量并得到所有1、2、3等的总和。
  • 你的行是用;分隔的吗? @SaneeshCS
  • 单个单元格中有多个变量,例如单元格一只有 1,单元格二有 1;2,单元格 3 有 1;2;3,等等。单个单元格中的所有变量都是用 ; 分隔
【解决方案3】:

这是我对这个问题的看法。我使用 tidyverse 是因为它会为我加载 stringr 和 tidyr

library(tidyverse)

id <- data.frame(Code = 1:7, #Make a coding data frame so you can label the results
                 Cause = c("Crop failure", "Water shortage", "Land degradation", "Lack of HH Labor", "Lack of income from agriculture", "Lack of manure / fertilizer", "Others"), stringsAsFactors = FALSE))

data <- Book1 %>% 
  separate(X1, into = paste0("X", 1:7), sep = ";") %>% #split the data by the ;, This induces NA that are removed later
  gather(key = "drop", value = "Code") %>% #put it into 1 column to exploit R's vectorization
  mutate(Code = as.integer(Code)) %>% #Make the code an integer for the join later
  filter(!is.na(Code)) %>% #remove those previous NAs
  group_by(Code) %>% 
  count() %>% # Counts 
  left_join(., id) #labels

colnames(data) <- c("Code", "Count", "Cause")

它会在单独的行发出警告,但它只是让您知道它正在用我们稍后删除的 NA 填充额外的单元格。您可能需要更改的唯一内容是 DataFrame 和 X1,具体取决于您为对象命名的内容。

这是我的结果的样子

 Code Count Cause                          
  <int> <int> <chr>                          
1     1     8 Crop failure                   
2     2     7 Water shortage                 
3     3     2 Land degradation               
4     4     4 Lack of HH Labor               
5     5     2 Lack of income from agriculture
6     6     2 Lack of manure / fertilizer    
7     7     1 Others  

希望有帮助!!

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多