从数据框中删除NA,而不删除整个行/列 [英] Remove NAs from data frame without deleting entire rows/columns
问题描述
我正在分析实验的一些试验数据,我们从190对组合中为参与者提供60对听觉刺激,以4分制进行评分.由于参与者每次对不同的对进行评分,因此我得到了很多缺失的值.
I'm analyzing some pilot data for an experiment where we are giving participants 60 pairs of auditory stimuli from a pool of 190 pairs to rate on a 4 point scale. I get a lot of missing values since the participants are rating different pairs each time.
我真的不在乎哪个参与者说什么,我只需要将同一对的所有评分都排在同一行即可,这样我就可以对n中每对的评分者之间的协议进行Light's Kappa测试 kappam.light (irr包).
I really don't care about which participant said what, I just need all the ratings for the same pair to be in the same row so I can perform a Light's Kappa test for inter-rater agreement on each pair in n with kappam.light (irr package).
这是我的15位参与者的数据的头,其中n是配对数,m是参与者:
Here is the head of my data for 15 participants, where n is the number of the pair and m is the participant:
> head(my.data)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
1 NA 1 NA 1 NA NA NA NA 2 2 NA NA NA 3 NA
2 NA 3 NA NA 3 NA NA NA 3 3 NA NA 4 NA 3
3 NA NA 1 NA NA 4 NA 1 NA NA 1 3 NA NA 3
4 NA NA 2 NA 1 NA NA 1 NA NA NA NA NA NA NA
5 1 NA NA 1 NA NA NA 1 NA NA 4 1 NA NA NA
6 2 NA NA NA 1 NA NA NA 1 3 NA NA NA 2 NA
我想要的输出(如果可能)如下:
The output I want (if possible) is the following:
[,1] [,2] [,3] [,4] [,5] [,6]
1 1 1 2 2 3
2 3 3 3 3 4 3
3 1 4 1 1 3 3
4 2 1 1
5 1 1 1 4 1
6 2 1 1 3 2
我不确定R是否会允许数据帧/矩阵中的行长变化,但是最好消除尽可能多的缺失值,所以
I'm not sure if R will allow varying row lengths in a data frame/matrix, but it would be great to get rid of as many missing values as possible so kappam.light won't just disregard the whole row.
推荐答案
您可以轻松摆脱list
中的NA
值.另一方面,matrix
和data.frame
都必须具有恒定的行长.这是执行此操作的一种方法:
You can easily get rid of NA
values in a list
. On the other hand, both matrix
and data.frame
need to have constant row length. Here's one way to do this:
# list removing NA's
lst <- apply(my.data, 1, function(x) x[!is.na(x)])
# maximum lenght
ll <- max(sapply(lst, length))
# combine
t(sapply(lst, function(x) c(x, rep(NA, ll-length(x)))))
这篇关于从数据框中删除NA,而不删除整个行/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!