将复杂列表转换为R中的data.frame

时间:2018-05-09 02:52:06

标签: r list dataframe

我有一个清单

"$1 $2"

我想将其转换为data.frame或matrix,其中值是行,而year是行名称,例如。

my_list = list("1" = list(year = 1900, values = c(1,2,3,4,5), 
         another_attribute = "hello"), "2" = list(year = 1901,
         values = c(11,12,13,14,15), another_attribute = "thankyou"))
my_list
$`1`
$`1`$year
[1] 1900

$`1`$values
[1] 1 2 3 4 5

$`1`$another_attribute
[1] "hello"


$`2`
$`2`$year
[1] 1901

$`2`$values
[1] 11 12 13 14 15


$`2`$another_attribute
[1] "thankyou"

如果可能,行名称为年份

my_df = rbind(my_list$'1'$values, my_list$'2'$values)

我想避免循环语句,我可以在循环中拉出每个属性,但我想知道是否有另一种更流畅的方式。因为这是一个很重要的清单。

6 个答案:

答案 0 :(得分:2)

使用lapply提取年份和值

library(magrittr)
lapply(my_list, "[", "values") %>% 
  as.data.frame() %>% 
  setNames(unlist(lapply(my_list, "[","year") )) %>% 
  t()

答案 1 :(得分:2)

首先,您的示例列表必须更正:

my_list = list("1" = list(year = 1900, values = c(1,2,3,4,5),
another_attribute = "hello"), "2" = list(year = 1901,values =
c(11,12,13,14,15), another_attribute = "thankyou"))

然后你为列表列表中的一个元素(所以第一个内部列表)写一个完全符合你想要的函数。

entry2df <- function(el) {
  df <- as.data.frame(t(el$values))
  rownames(df) <- el$year
  df
}

然后使用lapply(用于在列表的每个元素上应用函数并将结果收集到结果列表中)并使用Reduce执行rbind以绑定这些列表单个数据帧逐行到一个数据帧。

Reduce(rbind, lapply(my_list, entry2df))

结果:

     V1 V2 V3 V4 V5
1900  1  2  3  4  5
1901 11 12 13 14 15

答案 2 :(得分:1)

使用Here is the index.html with the button I am trying to link (signupbtn). <!DOCTYPE html> <html> <head> <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> `enter code here`<link rel="stylesheet" href="page.css"> </head> <body> <form action="action_page.php" style="border:1px solid #ccc"> <div class="container"> <h1>Welcome to Mission Connect!</h1> <p>Please create an account.</p> <hr> <label for="email"><b>Email</b></label> <br> <input type="text" placeholder="Email Address" name="email" required> <br> <br> <label for="password"><b>Password</b></label> <br> <input type="password" placeholder="New Password" name="password" required> <br> <br> <label for="password"><b>Repeat Password</b></label> <input type="password" placeholder="Re-enter New Password" name="password-repeat" required> <p>By creating an account you agree to our Terms & Privacy. </p> <div class="clearfix"> <button type="button" class="cancelbtn">Cancel</button> <a href="createprofile.html"><button type="submit" class="signupbtn">Create My Account</button> </a> </div> </div> </form> </body></html> And this is the html page (createprofile.html) I'm trying to link it to. <!DOCTYPE html> <html> <head> <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> <link rel="stylesheet" href="page.css"> <h1>Welcome to Your Mission Connect Profile. </h1> <br> Select Profile Picture: <br> <br> <form action="/action_page.php"> <input type="file" name="pic" accept="image/*"> <input type="submit"> </form> <br> <br> <form> First Name:<br> <input type="text" name="firstname"> <br> <br> Last Name:<br> <input type="text" name="lastname"> <br> <br> Gender: <br> <input type="radio" name="gender" value="male" checked> Male<br> <input type="radio" name="gender" value="female"> Female<br> <br> Location:<br> <input type="text" name="location"> <br> <br> Home Congregation:<br> <input type="text" name="homecongregation"> <br> <br> Status: <br> <input type="radio" name="status" value="Preparing" checked> Preparing (Pre-Missionary)<br> <input type="radio" name="status" value="InTheField"> In the Field ( (Current Missionary)<br> <input type="radio" name="status" value="Supporting"> Supporting (Missionary Supporter)<br> <br> <br> Language(s):<br> <textarea name="message" rows="10" cols="70"> </textarea> <br> <br> Location(s) of Interest:<br> <textarea name="message" rows="10" cols="70"> </textarea> <br> <br> Duration: <br> <input type="radio" name="duration" value="shortterm" checked> Short- Term<br> <input type="radio" name="duration" value="longterm"> Long-Term<br> <input type="radio" name="duration" value="undecided"> Undecided<br> <br> <br> Why are you interested in missions?:<br> <textarea name="message" rows="10" cols="70"> </textarea> <br> <br> Describe your past missions experience(s):<br> <textarea name="message" rows="10" cols="70"> </textarea> </form> How can I make these two pages link? 将部分拼凑在一起,Map到矩阵并制作data.frame:

rbind

固定data.frame(do.call(Map, c(rbind, my_list) )) # year values.1 values.2 values.3 values.4 values.5 another_attribute #1 1900 1 2 3 4 5 hello #2 1901 11 12 13 14 15 thankyou 以将my_list放入第一个和第二个列表中的位置:

another_attribute

答案 3 :(得分:1)

在基地R:

mat <- t(sapply(my_list,`[[`,"values"))
rownames(mat) <- sapply(my_list,`[[`,"year")

mat
#      [,1] [,2] [,3] [,4] [,5]
# 1900    1    2    3    4    5
# 1901   11   12   13   14   15

答案 4 :(得分:0)

# Extracting the values
my_df <- t(sapply(1:length(my_list), function(x) my_list[[x]]$values))
my_df
#       [,1] [,2] [,3] [,4] [,5]
# [1,]    1    2    3    4    5
# [2,]   11   12   13   14   15

# Making it a data.frame
my_df <- as.data.frame(my_df)

# Assining the correct row names
rownames(my_df) <- sapply(1:length(my_list), function(x) my_list[[x]]$year)
my_df
#       V1 V2 V3 V4 V5
# 1900  1  2  3  4  5
# 1901 11 12 13 14 15

答案 5 :(得分:0)

以下是tidyverse

的选项
library(tidyverse)
my_list %>% 
  map_df(~.x %>% 
             as_tibble %>%
             select(year, values) %>%
             mutate(rn = row_number())  )  %>% 
  spread(rn, values) %>% 
  as.data.frame %>% 
  column_to_rownames('year') %>%
  as.matrix %>%
  `colnames<-`(., NULL)
#     [,1] [,2] [,3] [,4] [,5]
#1900    1    2    3    4    5
#1901   11   12   13   14   15