R语言 物种列表长格式到宽格式

2nc8po8w  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(91)

我正在尝试使用R中的reshape()从长格式转换为宽格式。
我已经尝试了几次迭代的reshape()代码,但都没有成功,我也不知道哪里出错了。我尝试了以下方法:

reshape(idvar = c("Site_ID", "date.only", "am.pm", "reserve", "on.VS.off", "Wind_strength", 
                                                 "Cloud_cover", "Feeding_resources", "Moisture_conditions"), 
                    v.names = "English_Name", direction = "wide", data = data)

and

reshape(idvar = c("Site_ID", "date.only", "am.pm", "reserve", "on.VS.off", "Wind_strength", 
                                                 "Cloud_cover", "Feeding_resources", "Moisture_conditions"), 
                    timevar = "English_Name", direction = "wide", data = data)

字符串
原始数据格式、可复制代码和示例格式

origional <- structure(list(Site_ID = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L), date.only = c("11/12/2023", "11/12/2023", "11/12/2023", 
"11/12/2023", "11/12/2023", "11/12/2023", "11/12/2023", "11/12/2023", 
"11/12/2023", "11/12/2023", "11/12/2023", "11/12/2023"), am.pm = c("am", 
"am", "am", "am", "am", "am", "am", "pm", "pm", "pm", "pm", "pm"
), reserve = c("a", "a", "a", "b", "b", "b", "b", "a", "a", "b", 
"b", "b"), on.VS.off = c("on", "on", "on", "off", "off", "off", 
"off", "on", "on", "off", "off", "off"), Wind_strength = c("branches", 
"branches", "branches", "twigs", "twigs", "twigs", "twigs", "leaves", 
"leaves", "branches", "branches", "branches"), Cloud_cover = c(2L, 
2L, 2L, 1L, 1L, 1L, 1L, 0L, 0L, 4L, 4L, 4L), Feeding_resources = c("low", 
"low", "low", "medium", "medium", "medium", "medium", "low", 
"low", "medium", "medium", "medium"), Moisture_conditions = c("no rain", 
"no rain", "no rain", "last 24hrs", "last 24hrs", "last 24hrs", 
"last 24hrs", "no rain", "no rain", "last 24hrs", "last 24hrs", 
"last 24hrs"), English_name = c("Australian Magpie", "Silver Gull", 
"European Starling", "Australian Magpie", "Silver Gull", "Masked Lapwing", 
"Galah", "Australian Magpie", "Silver Gull", "Masked Lapwing", 
"Silver Gull", "Magpie Lark"), count = c(1L, 3L, 6L, 2L, 4L, 
1L, 5L, 1L, 1L, 2L, 4L, 2L)), class = "data.frame", row.names = c(NA, 
-12L))

origional
   Site_ID  date.only am.pm reserve on.VS.off Wind_strength Cloud_cover Feeding_resources Moisture_conditions      English_name count
1        1 11/12/2023    am       a        on      branches           2               low             no rain Australian Magpie     1
2        1 11/12/2023    am       a        on      branches           2               low             no rain       Silver Gull     3
3        1 11/12/2023    am       a        on      branches           2               low             no rain European Starling     6
4        2 11/12/2023    am       b       off         twigs           1            medium          last 24hrs Australian Magpie     2
5        2 11/12/2023    am       b       off         twigs           1            medium          last 24hrs       Silver Gull     4
6        2 11/12/2023    am       b       off         twigs           1            medium          last 24hrs    Masked Lapwing     1
7        2 11/12/2023    am       b       off         twigs           1            medium          last 24hrs             Galah     5
8        1 11/12/2023    pm       a        on        leaves           0               low             no rain Australian Magpie     1
9        1 11/12/2023    pm       a        on        leaves           0               low             no rain       Silver Gull     1
10       2 11/12/2023    pm       b       off      branches           4            medium          last 24hrs    Masked Lapwing     2
11       2 11/12/2023    pm       b       off      branches           4            medium          last 24hrs       Silver Gull     4
12       2 11/12/2023    pm       b       off      branches           4            medium          last 24hrs       Magpie Lark     2


所需的数据格式、可复制的代码和示例格式

desired <- structure(list(Site_ID = c(1L, 2L, 1L, 2L), date.only = c("11/12/2023", 
"11/12/2023", "11/12/2023", "11/12/2023"), am.pm = c("am", "am", 
"pm", "pm"), reserve = c("a", "b", "a", "b"), on.VS.off = c("on", 
"off", "on", "off"), Wind_strength = c("branches", "twigs", "leaves", 
"branches"), Cloud_cover = c(2L, 1L, 0L, 4L), Feeding_resources = c("low", 
"medium", "low", "medium"), Moisture_conditions = c("no rain", 
"last 24hrs", "no rain", "last 24hrs"), Australian.Magpie = c(1L, 
2L, 1L, NA), Silver.Gull = c(3L, 4L, 1L, 4L), European.Starling = c(6L, 
NA, NA, NA), Maked.Lapwing = c(NA, 1L, NA, 2L), Galah = c(NA, 
5L, NA, NA), Magpie.Lark = c(NA, NA, NA, 2L)), class = "data.frame", row.names = c(NA, 
-4L))

desired
  Site_ID  date.only am.pm reserve on.VS.off Wind_strength Cloud_cover Feeding_resources Moisture_conditions Australian.Magpie
1       1 11/12/2023    am       a        on      branches           2               low             no rain                 1
2       2 11/12/2023    am       b       off         twigs           1            medium          last 24hrs                 2
3       1 11/12/2023    pm       a        on        leaves           0               low             no rain                 1
4       2 11/12/2023    pm       b       off      branches           4            medium          last 24hrs                NA
  Silver.Gull European.Starling Maked.Lapwing Galah Magpie.Lark
1           3                 6            NA    NA          NA
2           4                NA             1     5          NA
3           1                NA            NA    NA          NA
4           4                NA             2    NA           2

odopli94

odopli941#

timevar是你获取名字的地方,而v.names是你获取值的地方。

reshape(origional, v.names = 'count', timevar = 'English_name', dir='wide', idvar = 1:8)

   Site_ID  date.only am.pm reserve on.VS.off Wind_strength Cloud_cover Feeding_resources
1        1 11/12/2023    am       a        on      branches           2               low
4        2 11/12/2023    am       b       off         twigs           1            medium
8        1 11/12/2023    pm       a        on        leaves           0               low
10       2 11/12/2023    pm       b       off      branches           4            medium
   Moisture_conditions count.Australian Magpie count.Silver Gull count.European Starling
1              no rain                       1                 3                       6
4           last 24hrs                       2                 4                      NA
8              no rain                       1                 1                      NA
10          last 24hrs                      NA                 4                      NA
   count.Masked Lapwing count.Galah count.Magpie Lark
1                    NA          NA                NA
4                     1           5                NA
8                    NA          NA                NA
10                    2          NA                 2

字符串

相关问题