Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.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 - Fatal编程技术网

将函数应用于R中数据集中的所有行(包含多个观察值的行)

将函数应用于R中数据集中的所有行(包含多个观察值的行),r,R,我有一个数据框,其中一列标识4名参与者,另一列仅由一个单元格组成,包含88个观察结果-下面是数据框的一个可复制示例: pIndex <- c(1,2,3,4) T1_AllSequence <- c("N11_d1p3_t0p4, N11_d1p3_t0p1, N11_d1p3_t0p3", "N11_d0p1_t0p4, N11_d0p1_t0p7, N11_d0p1_t0p5",

我有一个数据框,其中一列标识4名参与者,另一列仅由一个单元格组成,包含88个观察结果-下面是数据框的一个可复制示例:

pIndex <- c(1,2,3,4)
T1_AllSequence <- c("N11_d1p3_t0p4, N11_d1p3_t0p1, N11_d1p3_t0p3", 
                    "N11_d0p1_t0p4, N11_d0p1_t0p7, N11_d0p1_t0p5",
                    "N7_d1p3_t0p4, N7_d1p3_t0p6, N7_d1p3_t0p4",
                    "N7_d0p5_t0p5, N7_d0p5_t0p6, N7_d0p5_t0p5")
Data <- as.data.frame(cbind(pIndex, T1_AllSequence))
dput(Data)
# structure(list(pIndex = structure(1:4, .Label = c("1", "2", "3", 
#                                                   "4"), class = "factor"), T1_AllSequence = structure(c(2L, 1L, 
#                                                                                                         4L, 3L), .Label = c("N11_d0p1_t0p4, N11_d0p1_t0p7, N11_d0p1_t0p5", 
#                                                                                                                             "N11_d1p3_t0p4, N11_d1p3_t0p1, N11_d1p3_t0p3", "N7_d0p5_t0p5, N7_d0p5_t0p6, N7_d0p5_t0p5", 
#                                                                                                                             "N7_d1p3_t0p4, N7_d1p3_t0p6, N7_d1p3_t0p4"), class = "factor")), class = "data.frame", row.names = c(NA, 
#                                                                                                                                                                                                                                  -4L))
您可以在此处下载数据库以复制问题:


非常感谢您对我的建议。

我尝试使用dplyr、stringr和tidyr或tidyverse复制您想要的输出:

图书馆管理员 数据%>% mutateT1\u AllSequence=str\u splitT1\u AllSequence,%%>% unnestT1_所有序列%>% transmuteV1=str_removeT1_AllSequence, PID=pIndex, 订单=行号, 顶点=ifelsegreplN7,V1,7,11, 距离=当REPLD0P1,V1~1, grepld0p5,V1~5, grepld0p9,V1~9, 是的~13, 张力=当REPT0P0,V1~0, greplt0p1,V1~1, greplt0p2,V1~2, greplt0p3,V1~3, greplt0p4,V1~4, greplt0p5,V1~5, greplt0p6,V1~6, greplt0p7,V1~7, greplt0p8,V1~8, greplt0p9,V1~9, 对~10 返回

一个tibble:12x6 V1 PID阶顶点距离张力 1 N11_d1p3_t0p4 1 11 13 4 2 N11_d1p3_t0p1 1 2 11 13 1 3 N11_d1p3_t0p3 1 3 11 13 3 4 N11_d0p1_t0p4 2 4 11 1 4 5 N11_d0p1_t0p7 2 5 11 1 7 6 N11_d0p1_t0p5 2 6 11 1 5 7 N7_d1p3_t0p4 3 7 13 4 8 N7_d1p3_t0p6 3 8 7 13 6 9 N7_d1p3_t0p4 3 9 7 13 4 10 N7_d0p5_t0p5 4 10 7 5 5 11 N7_d0p5_t0p6 4 11 7 5 6 12 N7_d0p5_t0p5 4 12 7 5 5
请分享一个例子,包括你使用的数据的一个小例子。显示数据的一个好方法是使用dput。通常这里的人不喜欢下载外部数据库。关于你的轮廓函数:看看案例,什么时候应该避免那些嵌套的ifelse函数。你的代码更容易阅读。嗨,妮可,欢迎来到SO!如果你能把问题浓缩起来,你就更有可能得到有用的答案。例如,您可能希望创建一些虚拟数据,并创建一个更简单的示例,更简洁地显示您需要帮助解决的问题。更多信息请点击此处:@MartinGal感谢您的建议,我更新了问题,提供了一个可复制的示例。我还尝试了“case\u when”函数,但不幸的是,它不起作用。太感谢您了!!我确实尝试在我的函数中使用grepl,但由于某种原因,它不起作用。。。你的代码工作得很好!!!
#### Extracting variables from embedded data #### 

i = T1$pIndex

Contours <- #for(i in 1:nrow(T1)){
  function(i){
  c = as.character(Data[i,'T1_AllSequence'])
  Curvature <- as.data.frame(strsplit(c, ",")[[i]])
  Curvature <- dplyr::rename (Curvature,
                              V1 = `strsplit(c, ",")[[i]]`)
  Curvature <- mutate(Curvature,
                      pIndex = i,
                      order = as.integer(1:88),
                      vertex = ifelse(grepl("N7", V1), 7, 11),
                      distance = ifelse(grepl("d0p1", V1), 1,
                                        ifelse(grepl("d0p5", V1), 5,
                                               ifelse(grepl("d0p9", V1), 9, 13))),
                      tension = ifelse(grepl("t0p0", V1), 0,
                                       ifelse(grepl("t0p1", V1), 1,
                                              ifelse(grepl("t0p2", V1), 2,
                                                     ifelse(grepl("t0p3", V1), 3,
                                                            ifelse(grepl("t0p4", V1), 4,
                                                                   ifelse(grepl("t0p5", V1), 5,
                                                                          ifelse(grepl("t0p6", V1), 6,
                                                                                 ifelse(grepl("t0p7", V1), 7,
                                                                                        ifelse(grepl("t0p8", V1), 8,
                                                                                               ifelse(grepl("t0p9", V1), 9, 10)))))))))))
  return(Curvature)
}
require(plyr)
Stim <- ddply(T1, 1, Contours(T1))
Stim <- data.frame(t(apply(as.matrix(T1), 1, Contours)))
Stim <- apply(T1, 1, Contours())
Stim <- as.data.frame(apply(T1, 1, Contours))
Stim <- apply(T1[,442], 4, Contours)
             V1    PID order vertex distance tension
1 N11_d0p9_t0p0      1     1     11        9       0
2  N7_d1p3_t0p0      1     2      7       13       0
3 N11_d1p3_t0p3      1     3     11       13       3
4  N7_d0p5_t0p7      1     4      7        5       7
5  N7_d0p1_t0p1      1     5      7        1       1
6  N7_d0p9_t0p8      1     6      7        9       8