我正在尝试将从Qualtrics下载的数据导入R.它是一个csv文件。
但是,我遇到了2个问题。
<?php
include '../init.php';
if(isset($_POST['deleteTweet']) && !empty($_POST['deleteTweet'])){
$tweet_id = $_POST['deleteTweet'];
$user_id = $_SESSION['user_id'];
//get tweet data from tweet id
$tweet = $getFromT->tweetPopup($tweet_id);
//create link for tweet image to delete from
$imageLink = '../../'.$tweet->tweetImage;
//delete the tweet from database
$getFromT->delete('tweets', array('tweetID' => $tweet_id, 'tweetBy' => $user_id));
//check if tweet has image
if(!empty($tweet->tweetImage)){
//delete the file
unlink($imageLink);
}
}
if(isset($_POST['showpopup']) && !empty($_POST['showpopup'])){
$tweet_id = $_POST['showpopup'];
$user_id = $_SESSION['user_id'];
$tweet = $getFromT->tweetPopup($tweet_id);
?>
<div class="retweet-popup">
<div class="wrap5">
<div class="retweet-popup-body-wrap">
<div class="retweet-popup-heading">
<h3>Are you sure you want to delete this Tweet?</h3>
<span><button class="close-retweet-popup"><i class="fa fa-times" aria-hidden="true"></i></button></span>
</div>
<div class="retweet-popup-inner-body">
<div class="retweet-popup-inner-body-inner">
<div class="retweet-popup-comment-wrap">
<div class="retweet-popup-comment-head">
<img src="<?php echo $tweet->profileImage;?>"/>
</div>
<div class="retweet-popup-comment-right-wrap">
<div class="retweet-popup-comment-headline">
<a><?php echo $tweet->screenName;?> </a><span>@<?php echo $tweet->username . ' ' . $tweet->postedOn;?></span>
</div>
<div class="retweet-popup-comment-body">
<?php echo $tweet->status . ' ' .$tweet->tweetImage;?>
</div>
</div>
</div>
</div>
</div>
<div class="retweet-popup-footer">
<div class="retweet-popup-footer-right">
<button class="cancel-it f-btn">Cancel</button><button class="delete-it" data-tweet="<?php echo $tweet->tweetID;?>" type="submit">Delete</button>
</div>
</div>
</div>
</div>
</div>
<?php }?>
。但是,显然有些是character
,有些是date
,有些是factor
。 R如何才能正确地找出每列的数据类?integer
library(tidyverse)
filename <- "mydata.csv"
df = read_csv(filename, col_names = TRUE)
Parsed with column specification:
cols(
.default = col_character()
)
See spec(...) for full column specifications.
)和数据矩阵。不幸的是,使用header
参数不起作用。它说我的数据只有1个观察......为什么?skip = 3
filename <- "mydata.csv"
headers = read_csv(filename, col_names = FALSE, n_max = 1)
df = read_csv(filename, skip = 3, col_names = FALSE)
colnames(df)= headers
将csv文件导入R?
的好方法是什么?答案 0 :(得分:2)
我使用以下代码将Qualtrics中的数据导入R:
Error in names(x) <- value :
'names' attribute [273] must be the same length as the vector [1]
然而,有一点需要注意。 此方法仅在您下载数据时删除所有换行符时才有效。 (请参阅下图,了解如何执行此操作。)我的library(tidyverse)
filename <- "mydata.csv"
headers = read_csv(filename, col_names = FALSE, n_max = 1)
df = read_csv(filename, skip = 3, col_names = FALSE)
colnames(df)= headers
参数有效,因为我从Qualtrics下载数据时删除了所有换行符。您在Qualtrics中询问的问题很可能包含多行。 R以这种方式理解您的文件是一个问题。我建议您从网站下载数据时删除所有换行符。
使用上述方法,R通常可以正确识别大多数列的数据结构,为您节省大量精力进行自我重构。
答案 1 :(得分:0)
您还可以使用qualtRics软件包中的read_survey函数。它的另一个优点是可以缩短矩阵问题中某些很长的措词。另请参见此处:https://www.adrianbruegger.com/post/import-qualtrics-csv-files/